强制MKMapView viewForAnnotation更新

ing*_*.am 16 cocoa-touch mkmapview mkannotation mkannotationview ios

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

我已经阅读了许多类似的问题,我可以证实这一点 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;
}
Run Code Online (Sandbox Code Playgroud)

这是我添加引脚的代码:

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];
Run Code Online (Sandbox Code Playgroud)

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

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];
Run Code Online (Sandbox Code Playgroud)

我不确定我错过了什么.viewForAnnotation显然是有效的,它只是在初始添加之后才被调用!如果它是调用此函数我100%肯定它会工作,因为它重新启动应用程序的颜色变化!

编辑:哦,我真的不想开始删除注释并重新添加它们.这就是我在短期内所做的事情!

Zha*_*ang 42

实际上,我不知道这对你有用,但这就是我做的.

我不需要从地图中删除注释.我需要做的就是告诉地图给我一个参数注释的注释视图.地图将返回正确的注释.从那里,我有一个属性用于我的自定义注释,以确定它是否是一个活动项目,如果是,显示正常的图像,否则显示完整的图像.

-(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"];
    }
}
Run Code Online (Sandbox Code Playgroud)

有点晚,但希望它能帮助遇到这个问题的其他人.


Cyr*_*roy 15

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