Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
351 views
in Technique[技术] by (71.8m points)

ios - change UIImage from MKAnnotation in the MKMapView

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...