所以我有一个 MKMapView,其中添加了我的所有引脚,引脚的颜色取决于是否为该引脚设置了值。当我第一次加载应用程序时,调用 viewForAnnotation
并相应地设置颜色。但是,当我更新 pin 的详细信息(例如位置、标题等...)时,我也更新了 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;
}
这是我添加 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];
这是我更新 pin 的代码(不同的添加方法):
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% 确定它会正常工作,因为如果我重新启动应用程序,它会改变颜色!
编辑:哦,我真的不想开始删除注释并重新添加它们。无论如何,这就是我在短期内要做的事情!
最佳答案
实际上,我不知道这是否适合您,但我就是这样做的。
我不需要从 map 上删除注释。我需要做的就是告诉 map 给我参数注释的注释 View 。 map 将返回正确的注释。从那里,我有一个自定义注释的属性,用于识别它是否是事件项目,如果是,则显示普通图钉图像,否则显示完整图钉图像。
-(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"];
}
}
有点晚了,但希望它能帮助遇到这个问题的其他人。
关于ios - 强制 MKMapView viewForAnnotation 更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10774002/