问题
I have an array of objects that conform to <MKAnnotation>
.
I load this array into my annotations using addAnnotations:.
In the method:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:
(id<MKAnnotation>)annotation
I have every pin load a custom image using:
annotationView.image = [UIImage imageNamed:@"purp_pin.png"];
However, I don't want all the pins to load with this image. I want it to load a different custom image/identifier depending on the properties the object that conformed to <MKAnnotation>
had.
How would I do this?
回答1:
If you have some custom property in your object that conforms to MKAnnotation
, one way you can access it in viewForAnnotation
to set the image is like this:
MyAnnotationClass *myAnnot = (MyAnnotationClass *)annotation;
if (myAnnot.someProperty == 42)
annotationView.image = [UIImage imageNamed:@"purp_pin.png"];
else
annotationView.image = [UIImage imageNamed:@"default.png"];
Make sure the image
property is set regardless of whether annotation view is being dequeued or created.
来源:https://stackoverflow.com/questions/7288757/how-do-i-load-different-custom-pins-or-identifiers-based-off-of-their-property-v