Random MKAnnotationView is tapped when there are overlapping annotations

Posted by Alexandre Gellibert on Stack Overflow See other posts from Stack Overflow or by Alexandre Gellibert
Published on 2010-03-18T15:07:44Z Indexed on 2010/03/18 15:11 UTC
Read the original article Hit count: 773

I cannot believe this bug/problem doesn't have any solution!

In my iphone application, I'm using MapKit with MKMapView and custom MKAnnotationView.

The problem is when annotations overlap (in my app, annotations are photos and those photos may overlap) and when you tap on the annotation that appears on front, other annotation (on back) receives the event (seems to be random).

I didn't find any way to send the event to the front annotation.

Z ordering and Order of overlapping annotations questions on stackoverflow did not help me that much.

Please any idea is welcome (even ugly ones)!

Here's some of my code (nothing fancy, very common):

CustomAnnotation.h

@interface CustomAnnotation : NSObject <MKAnnotation> {
   @private
   CustomAnnotationView* view;
}

    @property (nonatomic, retain) CustomAnnotationView* view;

@end

CustomAnnotation.m

@implementation CustomAnnotation

@synthetize view;

CustomAnnotationView.h

@interface CustomAnnotationView : MKAnnotationView {
}

@end

CustomAnnotationView.m

@implementation CustomAnnotationView

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
// Do something related to the annotation tapped
}

@end

Main class ... // Annotations are added and some of them overlaps with others.

- (void)addAnnotation:(CustomAnnotation*)annotation {
    [map addAnnotation:annotation];
}

...

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)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;
}

© Stack Overflow or respective owner

Related posts about iphone

Related posts about mapkit