change UIImage from MKAnnotation in the MKMapView

删除回忆录丶 提交于 2019-11-27 08:33:40

问题


I have a mapView with only on MKAnnotation, that has a costume image. When the user changes the mapType, I need to change the image of that annotation.

The way I did that was to remove the annotation from the map, and insert another with the correct image, bu i don't think is the best way. It takes about 1 ou 2 seconds to show the new image.

How can I do it without remove the annotation and drop another?

Thanks,

RL


回答1:


You can use the viewForAnnotation: instance method of the map view (not the same as its delegate method with a similar name) to get the current view of the annotation and force the image change explicitly.

For example, at the place where the map type is changed:

MKAnnotationView *av = [mapView viewForAnnotation:annotation];

if (mapView.mapType == MKMapTypeHybrid)
    av.image = [UIImage imageNamed: @"hybrid.png"];
else
    av.image = [UIImage imageNamed: @"standard.png"];

However, you should add the exact same if-statement to the viewForAnnotation delegate method also so when the map view later calls the delegate method itself, it will set the correct image also.

You may want to move the image-setting logic to a common method that you can call from the place where you change the map type and from the viewForAnnotation delegate method (the MKAnnotationView object would be passed as a parameter). If the logic is in one place, you don't have to remember to keep both places in sync.



来源:https://stackoverflow.com/questions/6808792/change-uiimage-from-mkannotation-in-the-mkmapview

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