cannyboy
cannyboy

Reputation: 24406

Changing an MKAnnotationView custom image?

I know you can create a custom annotation view using something like:

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    MKPinAnnotationView *annotationView = [[[CustomAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"CustomAnnotation"] autorelease];     
    annotationView.image = [UIImage imageNamed:@"customPin.png"];
    return annotationView;
}

.. but how do I change the image in other parts of my code.. (after it has been created with the above)?

Upvotes: 0

Views: 1940

Answers (1)

rdurand
rdurand

Reputation: 7410

You probably don't need the answer anymore, but still, the question is unanswered. What I usually do is add a property to the annotation, telling which image should be used. It can be a BOOL, a UIImage, or pretty much whatever you like.

In viewForAnnotation, I check for that value and set the appropriate image.

Whenever I want to update the image, I change the property's value, and I remove and add the annotation :

[theMapView removeAnnotation: myAnnotation];
[theMapView addAnnotation: myAnnotation];

That way, the annotation is re-drawn.

Upvotes: 2

Related Questions