强制更新MKMapView viewForAnnotation [英] Force MKMapView viewForAnnotation to update

查看:72
本文介绍了强制更新MKMapView viewForAnnotation的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个添加了所有引脚的MKMapView,并且引脚的颜色取决于是否为该引脚设置了值.当我第一次加载应用程序时,会调用 viewForAnnotation 并相应地设置颜色.但是,当我更新图钉的详细信息(例如位置,标题等)时,我也会更新图钉颜色以发现它没有更新.初始添加后,似乎没有再次调用 viewForAnnotation .

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 is called and the colours are set accordingly. However, when I update the pin's details (such as location, title, etc...) I also update the pinColour to find it doesn't update. It looks like viewForAnnotation isn't called again after the initial add.

我已经阅读了许多与此类似的问题,并且可以确认 mapView.delegate = self;

I have read many questions similar to this and I can confirm that mapView.delegate = self;

这是我的 viewForAnnotation 代码:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(MapAnnotation *)annotation
{
    if([annotation class] == MKUserLocation.class)
        return nil;

    NSString *pinIdentifier = annotation.identifier; // This is a unique string for each pin and is getting populated every time!

    MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [mapView dequeueReusableAnnotationViewWithIdentifier:pinIdentifier];

    if(annotationView == nil)
        annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pinIdentifier];
    else
        annotationView.annotation = annotation; // Never had this line fire...

    annotationView.canShowCallout = YES;
    annotationView.animatesDrop = NO;
    annotationView.enabled = YES;
    annotationView.tag = annotation.counter;

    if(annotation.pinColour == Stopped) // from enum
        annotationView.pinColor = MKPinAnnotationColorRed;
    else
        annotationView.pinColor = MKPinAnnotationColorGreen;

    UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    [infoButton addTarget:self action:@selector(mapCalloutButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    infoButton.tag = annotation.counter;
    annotationView.rightCalloutAccessoryView = infoButton;

    return annotationView;
}

这是我添加图钉的代码:

Here is the code where I add the pin:

CLLocationCoordinate2D annotationCoord;
annotationCoord.latitude = latestPosition.latitude;
annotationCoord.longitude = latestPosition.longitude;

MapAnnotation *annotation = [[MapAnnotation alloc] init];
annotation.coordinate = annotationCoord;
annotation.identifier = theIdentifier;
annotation.title = theTitle;
annotation.subtitle = theSubtitle
annotation.pinColour = [self getPinColour];
annotation.counter = theCounter;

[theMapView addAnnotation:annotation];

这是我更新引脚的代码(不同的添加方法):

Here is the code where I update the pin (different method to add):

updatePin = true;
pinCounter = mapPin.counter;

CLLocationCoordinate2D annotationCoord;
annotationCoord.latitude = latestPosition.latitude;
annotationCoord.longitude = latestPosition.longitude;
[mapPin setCoordinate:annotationCoord];

mapPin.identifier = theIdentifier;
mapPin.subtitle = theSubtitle;
mapPin.pinColour = [self getPinColour];

我不确定我缺少什么. viewForAnnotation 显然可以正常工作,只是在最初添加之后从未调用过它!如果要调用此函数,我100%确信它会工作,因为重新启动应用程序后它会改变颜色!

I'm not sure what I'm missing. viewForAnnotation is obviously working, it's just not ever called after the initial add! If it were to call this function I'm 100% sure it would work as it does the colour change if I restart the app!

哦,我真的不想开始删除注释并重新添加它们.无论如何,这就是我短期内要做的事情!

Oh and I really don't want to start removing annotations and re-adding them. It's what I'm doing in the short term anyway!

推荐答案

由于地图视图缓存其批注的方式,如果需要更改其外观,则需要删除并重新添加批注.一个简单的删除和添加是要走的路.除此之外,没有缓存失效机制.

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.

这篇关于强制更新MKMapView viewForAnnotation的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆