0

我已经为我的所有地图图钉创建了一个自定义 MKPinAnnotationView,但是我在自定义用户位置图钉时遇到了问题。如果我为它创建了一个自定义图钉,那么我会丢失蓝色脉动点,并用我不想要的图钉替换它。所以我在 viewForAnnotation 方法中返回 nil:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{   
    if(annotation == self.mapView.userLocation)
    {
        // show the blue dot for user's GPS location
        return nil;
    }

    ... code for custom annotation pins goes here
}

但是,我确实希望它在我点击它时调用自定义标注视图。选择蓝点后,我尝试添加子视图:

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{    
    if(view == [self.mapView viewForAnnotation:self.mapView.userLocation])
    {
        MKAnnotationView *userLocationView = [self.mapView viewForAnnotation:self.mapView.userLocation];

        userLocationCalloutView = [[MyAnnotationCalloutView alloc] initWithFrame:CGRectMake(-113, -58, 247, 59)];
        [userLocationCalloutView.rightAccessoryButton addTarget:self action:@selector(pinButtonTapped) forControlEvents:UIControlEventTouchDown];
        [userLocationView addSubview:userLocationCalloutView];
    }
}

但问题在于标注附件按钮位于点击区域之外,我无法添加 hitTest:withEvent 因为蓝点注释视图是私有的......有什么想法吗?

4

1 回答 1

2

您可以在委托方法 mapView:didAddAnnotationViews 中为 userLocation 注释获取构建的 MKAnnotationView:

- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views
{
    for(MKAnnotationView *view in views)
    {
        if (view.annotation == mapView.userLocation)
        {
            view.canShowCallout = YES;
            view.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
            break;
        }
    }
}
于 2012-08-07T23:51:28.457 回答