0

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.

Panos
  • 7,227
  • 13
  • 60
  • 95

1 Answers1

5

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

Vikings
  • 2,527
  • 32
  • 45
  • I managed to get the selected pin using the accessoryControlTapped method you told me. The problem now is that my annotation is a custom MKPinAnnotation ( sorry if I mispelled) and it has a property called objectId. I want to get that Id. So i try to Cast the selected annotation to MapPinAnnotation and then get the id, but it returns null. – Panos May 18 '12 at 20:24
  • The Code I'm using is : ' - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control { NSLog(@"The annotation tapped is: %@", view.annotation.title); if ( [view.annotation isKindOfClass:[MapPinViewAnnotation class]] ) { MapPinViewAnnotation *selectedAnnotation = (MapPinViewAnnotation *) view.annotation; selectedId = selectedAnnotation.objectId; NSLog(@"Selected ID is : %@" , selectedId); //do something and go to next view code here } }' – Panos May 18 '12 at 20:24