squeezer123
squeezer123

Reputation: 191

MKMapView: Get clicked event on annotation pin

I am using an MKMapView containing a couple of MKAnnotation pins.
Above the map I am showing a UITableView with detailed information of the MKAnnotation pins.

My problem: When I select a pin, I would like to select the corresponding table cell. For this I would like to catch an event/delegate if the pin is selected. I am not talking about calling the callout accessory

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

Upvotes: 19

Views: 28155

Answers (3)

software evolved
software evolved

Reputation: 4352

Just an update to this -- in iOS 4 there are MKMapViewDelegate methods that can be used to track annotation selection and de-selection:

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view

Upvotes: 42

user207616
user207616

Reputation:

you can use an Observer for Selected-Event:

[pin addObserver:self
      forKeyPath:@"selected" 
         options:NSKeyValueObservingOptionNew
         context:@"ANSELECTED"];

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{

    NSString *action = (NSString*)context;

    if([action isEqualToString:@"ANSELECTED"]){

        BOOL annotationAppeared = [[change valueForKey:@"new"] boolValue];
        if (annotationAppeared) {
            // clicked on an Annotation
        }
        else {
            // Annotation disselected
        }
    }
}

Upvotes: 3

Will Harris
Will Harris

Reputation: 21695

I haven't seen a simple way to do this in MapKit. There's no mapView:annotationWasTapped: on the delegate.

One way to do it would be to provide your own annotation view subclass. The custom annotation view could capture the pin selection in setSelected:animated: or in a lower level event handler and pass that information up to your view controller.

Upvotes: 1

Related Questions