Question

Ok, so you typically have some object X you want to be annotated inside a MKMapView. You do this way:

DDAnnotation *annotation = [[DDAnnotation alloc] initWithCoordinate: poi.geoLocation.coordinate title: @"My Annotation"];
[_mapView addAnnotation: annotation];

Then you create the annotation view inside

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation;

And when some callout gets tapped, you handle the event inside:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control;

What's the cleanest solution to pass X down to the latest tap event?

Was it helpful?

Solution

If I'm understanding your question, you should add a reference or property to your DDAnnotation class so that in your calloutAccessoryControlTapped method you can access the object.

@interface DDAnnotation : NSObject <MKAnnotation> {
    CLLocationCoordinate2D coordinate;
    id objectX;
}
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
@property (nonatomic, retain) id objectX;

When you create the annotation:

DDAnnotation *annotation = [[DDAnnotation alloc] initWithCoordinate:poi.geoLocation.coordinate title: @"My Annotation"];
annotation.objectX = objectX;
[_mapView addAnnotation: annotation];

Then:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control{

    DDAnnotation *anno = view.annotation;
    //access object via
    [anno.objectX callSomeMethod];
}

OTHER TIPS

I did this and it worked alright!

It's exactly what I need because I needed to do something when the map was tapped but letting the tap into the annotation flow normally.

- (void)viewDidLoad
{
    [super viewDidLoad];
    UIGestureRecognizer *g = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)] autorelease];
    g.cancelsTouchesInView = NO;
    [self.mapView addGestureRecognizer:g];

}

- (void) handleGesture:(UIGestureRecognizer*)g{
    if( g.state == UIGestureRecognizerStateEnded ){
        NSSet *visibleAnnotations = [self.mapView annotationsInMapRect:self.mapView.visibleMapRect];
        for ( id<MKAnnotation> annotation in visibleAnnotations.allObjects ){
            UIView *av = [self.mapView viewForAnnotation:annotation];
            CGPoint point = [g locationInView:av];
            if( [av pointInside:point withEvent:nil] ){
                // do what you wanna do when Annotation View has been tapped!
                return;
            }   
        }
        //do what you wanna do when map is tapped
    }
 }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top