MKAnnotationView fault when zoom in/out changed the pin image

女生的网名这么多〃 提交于 2019-12-02 01:40:33

The custAttr variable (which you are setting outside the delegate method) will not always be in sync with the annotation that the viewForAnnotation delegate method is called for.

The delegate method is not necessarily called right after addAnnotation or addAnnotations and can be called multiple times for each annotation if the map needs to display the annotation view again after a zoom or pan.

When it gets called again for the same annotation, the custAttr variable no longer matches up.


You need to add a custAttr property (I suggest using a different name) to your MapAnnotation class and set it when creating the annotation (before calling addAnnotation).

For example:

MapAnnotation *ann = [[MapAnnotation alloc] init];
ann.coordinate = ...
ann.title = ...
ann.subtitle = ...
ann.custAttr = custAttr; // <-- copy to the annotation object itself
[mapView addAnnotation:ann];


Then, in viewForAnnotation, read the custAttr property from the annotation parameter (after casting it to MapAnnotation *) instead of referencing the externally declared custAttr.

You may want to use a different name for the custAttr property in MapAnnotation to avoid confusion.

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