MKAnnotationView fault when zoom in/out changed the pin image

后端 未结 1 1934
轻奢々
轻奢々 2021-01-23 01:02

I have used annotation map and used more than one image for the pins but whenever I zoom in or zoom out, it changes all the pins to one image.

I get the locations from a

1条回答
  •  说谎
    说谎 (楼主)
    2021-01-23 01:22

    The custAttr variable (which you are setting outside the delegate method) will not always be in sync with the annotation that the viewForAnnotation delegate method is called for.

    The delegate method is not necessarily called right after addAnnotation or addAnnotations and can be called multiple times for each annotation if the map needs to display the annotation view again after a zoom or pan.

    When it gets called again for the same annotation, the custAttr variable no longer matches up.


    You need to add a custAttr property (I suggest using a different name) to your MapAnnotation class and set it when creating the annotation (before calling addAnnotation).

    For example:

    MapAnnotation *ann = [[MapAnnotation alloc] init];
    ann.coordinate = ...
    ann.title = ...
    ann.subtitle = ...
    ann.custAttr = custAttr; // <-- copy to the annotation object itself
    [mapView addAnnotation:ann];
    


    Then, in viewForAnnotation, read the custAttr property from the annotation parameter (after casting it to MapAnnotation *) instead of referencing the externally declared custAttr.

    You may want to use a different name for the custAttr property in MapAnnotation to avoid confusion.

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