Issue with overlapping annotations (MKAnnotationView) on map

本秂侑毒 提交于 2019-12-03 12:18:59

Finally, the best way I found and that avoids loosing map funcionalities is to:

  • let the touch listener on CustomAnnotationView (touchesEnded) (on each annotation)

  • when receiving touch event, transfer the event to main class

  • main class loops on annotations and keeps the annotation that has a good location (touch event in the annotation frame) and that is in front

It works pretty well.

smountcastle

See the solution to this question. Basically you'll create a transparent UIView which will intercept all of the touches. I haven't tried it, but this may be more trouble than it's worth if you have to reimplement all of the pinch-to-zoom functionality in MKMapView.

If you have two (or more) MKAnnotations co-located you can repeatedly tap to toggle between the two. That should work without any changes to your app.

Following up on Alexandre's answer, I disabled all my custom MKAnnotationViews objects with

annView.enabled = NO

so they don't respond to the MKMapView default select behavior, and implemented in my custom MKAnnotationView the following:

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
 self.enabled = YES;
 [(MKMapView*)self.superview selectAnnotation:self.annotation animated:NO];
}

This solution works great on iOS 4. It seems like disabling an MKAnnotationView doesn't disable its userInteractionEnabled, whereas on iOS 3 it does. Still looking for a solution for iOS 3...

EDIT: actually, this seems to be a bug in iOS 4.1. In iOS 4.2, disabling a pin also disables the touch events. Therefore this method only works for 4.1 and possibly 4.0.

I've added the UITapGestureRecognizer to the view at the front and that solved the issue for me.

In your case, adding a gesture recognizer to CustomAnnotationView should solve the issue.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!