Force MKMapView viewForAnnotation to update

后端 未结 5 1062
忘掉有多难
忘掉有多难 2021-02-12 22:05

So I have a MKMapView with all my pins added, and the colour of the pin is dependent on whether a value is set for that pin. When I first load the app, viewForAnnotation

相关标签:
5条回答
  • 2021-02-12 22:35

    Swift 2.1:

    I had the same issue, and found a quick solution, trigger this when needed, also sending it to the main thread would be wise:

    var annotationsArray = mapView.annotations
    mapView.removeAnnotations(mapView.annotations)
    mapView.addAnnotations(arrayIncs)
    arrayIncs.removeAll()
    
    0 讨论(0)
  • 2021-02-12 22:39

    Due to the way the map view caches its annotations, you NEED to remove and re-add the annotation if you need to make changes to its appearance. A simple remove & add is the way to go. There is no cache invalidating mechanism but this.

    0 讨论(0)
  • 2021-02-12 22:43

    Actually, I dont' know if this worked for you but this is how I did it.

    I didn't need to delete the annotation from map. All I need to do is tell the map to give me the annotation view for a parameter annotation. The map will return the correct annotation. From there, I have a property for my custom annotation to identify whether it is an active item, if yes, show the normal pin image, else show full pin image.

    -(void)updateAnnotationImage:(CustomAnnotation *)paramAnnotation
    {
        MKAnnotationView *av = [geoMap viewForAnnotation:paramAnnotation];
    
        if (paramAnnotation.active)
        {
            av.image = [UIImage imageNamed:@"PinNormal.png"];
        }
        else
        {
            av.image = [UIImage imageNamed:@"PinFull.png"];
        }
    }
    

    Bit late but hopefully it helps others who came across this problem.

    0 讨论(0)
  • 2021-02-12 22:43

    I also found this answer helpful: In which case that mapView:viewForAnnotation: will be called?

    Whenever you call addAnnotation method

    - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id < MKAnnotation >)annotation gets called. 
    
    0 讨论(0)
  • 2021-02-12 22:44

    Just spent a couple of hours to get this to work on Xamarin; this is a warning for other Xamarin developers. Make sure you use the ViewForAnnotation method and not the GetViewForAnnotation delegate. I was using the wrong method which returned new annotation views instead of the existing ones... of course it wasn't working!

    0 讨论(0)
提交回复
热议问题