MKMapView Off Screen Annotation Image Incorrect

て烟熏妆下的殇ゞ 提交于 2019-12-02 05:37:11

The dequeueReusableAnnotationViewWithIdentifier may return an annotation view that was used for an annotation different from the current annotation parameter.

If the dequeueReusableAnnotationViewWithIdentifier is succesful (ie. you're using a previously-used annotation view), you must update its annotation property to be sure the view matches the current annotation's properties.

So try changing this part:

MKAnnotationView *annView = (MKAnnotationView *)[self.mainMapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (!annView) {
    annView = [[MKAnnotationView alloc] initWithAnnotation:annotation
                                                      reuseIdentifier:identifier];
}
MapAnnotationViewController *sac = (MapAnnotationViewController *)annView.annotation;

to:

MKAnnotationView *annView = (MKAnnotationView *)[self.mainMapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (!annView) {
    annView = [[MKAnnotationView alloc] initWithAnnotation:annotation
                                                      reuseIdentifier:identifier];
}
else {
    annView.annotation = annotation; // <-- add this
}

MapAnnotationViewController *sac = (MapAnnotationViewController *)annView.annotation;


Another potential issue (not causing the problem in the question) is that the view's image property is only set if relationshipParam is one of three values.

If somehow relationshipParam is not one of those three coded values and the view was dequeued, the image will be based on some other annotation's relationshipParam.

So you should add an else part to the section that sets image and set it to some default image just in case:

...
else if ([sac.relationshipParam isEqualToString:@"paramC"])
{
    annView.image = [UIImage imageNamed:@"image3.png"];
}
else
{
    annView.image = [UIImage imageNamed:@"UnknownRelationship.png"];
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!