Note that there are some explanatory texts on larger screens.

plurals
  1. POIssue with overlapping annotations (MKAnnotationView) on map
    text
    copied!<p>In my iphone application, I'm using MapKit with MKMapView and custom MKAnnotationView.</p> <p>The problem is when annotations overlap on map (in my app, annotations are photos and those photos may overlap) and when you tap on the annotation that appears on front, it's another annotation (on back) which receives the event (seems to be random).</p> <p>I didn't find any way to send the event to the front annotation. I cannot believe this bug/problem doesn't have any solution!</p> <p><a href="https://stackoverflow.com/questions/1681565/z-ordering-of-mkannotationviews">Z ordering</a> and <a href="https://stackoverflow.com/questions/1145238/how-to-define-the-order-of-overlapping-mkannotationviews">Order of overlapping annotations</a> questions on stackoverflow did not help me that much.</p> <p>Please any idea is welcome (even dirty solutions)!</p> <p>Here's some of my code (nothing fancy, very common):</p> <p>CustomAnnotation.h</p> <pre><code>@interface CustomAnnotation : NSObject &lt;MKAnnotation&gt; { @private CustomAnnotationView* view; } @property (nonatomic, retain) CustomAnnotationView* view; @end </code></pre> <p>CustomAnnotation.m</p> <pre><code>@implementation CustomAnnotation @synthetize view; </code></pre> <p>CustomAnnotationView.h</p> <pre><code>@interface CustomAnnotationView : MKAnnotationView { } @end </code></pre> <p>CustomAnnotationView.m</p> <pre><code>@implementation CustomAnnotationView - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ // Do something related to the annotation tapped } @end </code></pre> <p>Main class ... // Annotations are added and some of them overlaps with others.</p> <pre><code>- (void)addAnnotation:(CustomAnnotation*)annotation { [map addAnnotation:annotation]; } ... - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id &lt;MKAnnotation&gt;)annotation { NSString* identifier = getIdentifierFromAnnotation(annotation); CustomAnnotationView* view; if(!(view = (CustomAnnotationView*)[map dequeueReusableAnnotationViewWithIdentifier:identifier])) { view = [[CustomAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier: identifier]; [(CustomAnnotation*)annotation setView:view]; [view release]; } return view; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload