Search code examples
iphoneios4mkmapviewmkannotationmkannotationview

iphone Detect selected MKPinAnnotation in a MapView


I have a custom MKPinAnnotation in which I have added a couple of peroperties , like the Id of the object and the type. What I want is when someone selects a pin in the MapView to detect which pin is selected and get that data.

And Show a button on its view to use that data in the button's action.

Can anyone help with this? I can't find how to detect if (and which) an annotation is selected.


Solution

  • There is already a built in method to take care of this situation for you. You need to use the method below. The view.annotation is the annotation that was tapped.

    - (void)mapView:(MKMapView *)mapView 
     annotationView:(MKAnnotationView *)view
    calloutAccessoryControlTapped:(UIControl *)control
    {
        NSLog(@"The annotation tapped is: %@", view.annotation.title);
    }
    

    Edit: It can be found here, the API is your friend.

    https://developer.apple.com/library/ios/#documentation/MapKit/Reference/MKMapViewDelegate_Protocol/MKMapViewDelegate/MKMapViewDelegate.html#//apple_ref/doc/uid/TP40008204