How do I load different custom pins or identifiers based off of their property values?

荒凉一梦 提交于 2019-12-13 05:17:51

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!