Search code examples
objective-ciosmkmapviewmkannotationmkannotationview

Changing MKAnnotation view without removing the MKAnnotation


I have a callout on an MKAnnotation. The accessory on the callout should, when tapped, change the MKAnnotationView's image. Is there a way to change this, without recreating the MKAnnotation? The reason I ask is that I would like to change the image, without removing the callout. But obviously, the callout gets removed when I remove the annotation. So how do I simply change the image, so that the callout does not get removed?


Solution

  • Your MKMapViewDelegate has the methods mapView:didSelectAnnotationView: and mapView:didDeselectAnnotationView:. You are passed the MKAnnotationView here, and you can modify it here. Put code like this in your delegate:

    - (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {
        view.image = [UIImage imageNamed:@"selectedImage.png"];
    }
    
    - (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view {
        view.image = [UIImage imageNamed:@"pinImage.png"];
    }