Updating MKannotation image without flashing

前端 未结 3 1691
孤独总比滥情好
孤独总比滥情好 2021-02-06 19:33

I want to update the images of some of my annotations on a mapview every 5 seconds, however I dont\' want to remove and re-add them to the map as this causes them to \'flash\' o

相关标签:
3条回答
  • 2021-02-06 19:40

    Swift version of Anna answer:

    annotation.customImage = ... //the new image
    let av = self.mapView.viewForAnnotation(dnwl!)
    av?.image = annotation.customImage
    
    0 讨论(0)
  • 2021-02-06 19:48

    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;
    
    0 讨论(0)
  • 2021-02-06 19:48

    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];
    }
    
    0 讨论(0)
提交回复
热议问题