我已经为我的所有 map 图钉创建了一个自定义 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
}
但是,我确实希望它在我点击它时调用自定义标注 View 。选择蓝点后,我尝试添加 subview :
- (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 因为蓝点注释 View 是私有(private)的...有什么想法吗?
最佳答案
您可以在委托(delegate)方法 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;
}
}
}
关于ios - 如何为 MapKit 用户位置创建自定义标注 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10284021/