Updating MKannotation image without flashing

前提是你 提交于 2019-12-03 05:09:50

In the case where the annotation is not nil, instead of adding and removing, try this:

annotation.customImage = ... //the new image
MKAnnotationView *av = [self.mapView viewForAnnotation:annotation];
av.image = annotation.customImage;

Swift version of Anna answer:

annotation.customImage = ... //the new image
let av = self.mapView.viewForAnnotation(dnwl!)
av?.image = annotation.customImage

It seems you are using your own custom views for the annotations, in that case you can simply add a "refresh" method to your custom view and call it after you have updated the underlying annotation (ie: a custom view -a derived class from MKAnnotationView- is always attached to a potentially custom "annotation" class that conforms to the MKAnnotation protocol)

*) CustomAnnotationView.h

@interface CustomAnnotationView : MKAnnotationView
{
    ...
}
...

//tell the view to re-read the annotation data it is attached to
- (void)refresh;

*) CustomAnnotationView.m

//override super class method
- (void)setAnnotation:(id <MKAnnotation>)annotation
{
    [super setAnnotation:annotation];
    ...
    [self refresh];
}
- (void)refresh
{
    ...
    [self setNeedsDisplay]; //if necessary
}

*) Where you handle the MKMapView and its annotations

for(CustomAnnotation *annotation in [m_MapView annotations])
{
    CustomAnnotationView *annotationView = [m_MapView viewForAnnotation:annotation];
    [annotationView refresh];
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!