Changing an MKAnnotationView custom image?
I know you can create a custom annotation view using something like:
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation开发者_如何学Go>)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)?
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.
精彩评论