我使用了注释图并为图钉使用了不止一张图像,但是每当我放大或缩小时,它会将所有图钉更改为一张图像。
我从 Web 服务获取位置并识别它们,我使用字符串 (CustAttr
) 作为“T”或“P”。
问题是来自 Web 服务的最后一次调用使 CustAttr = T
当我放大或缩小时,它会调用 mapView viewForAnnotation
方法并将它们全部绘制为T
和所有 P
引脚被改变。
这是该方法的代码:
-(MKAnnotationView*) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[MKUserLocation class]]) {
return nil;
}
static NSString* AnnotationIndentifer = @"AnnotationIdentifier";
if ([custAttr isEqualToString:@"T"]) // ATMs
{
MKAnnotationView* pinView;
pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIndentifer];
MapAnnotation* mapAnnotation = annotation;
pinView.canShowCallout = YES;
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
pinView.rightCalloutAccessoryView = rightButton;
if (mapAnnotation.isClosest) {
pinView.image = [UIImage imageNamed:@"Closest_ATM.png"];
}
if (mapAnnotation.isOffline) {
pinView.image = [UIImage imageNamed:@"Offline_ATM.png"];
}
pinView.annotation = annotation;
return pinView;
}else if ([custAttr isEqualToString:@"P"]) // POIs
{
MKAnnotationView* pinView;
pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIndentifer];
pinView.canShowCallout = YES;
pinView.image = [UIImage imageNamed:@"Location_POI.png"];
pinView.annotation = annotation;
return pinView;
}
return nil;
}
我该如何解决这个问题?有没有一种方法可以阻止它在放大/缩小时调用此方法,或者是否有另一种方法让它像在同一图像中一样再次绘制它们?
最佳答案
custAttr
变量(您在委托(delegate)方法之外设置)不会总是与 annotation
同步。该viewForAnnotation
委托(delegate)方法被调用。
委托(delegate)方法不一定在 addAnnotation
之后立即调用或 addAnnotations
如果 map 需要在缩放或平移后再次显示注释 View ,则可以为每个注释调用多次。
当它再次被同一个注解调用时,custAttr
变量不再匹配。
您需要添加 custAttr
属性(我建议使用不同的名称)到您的 MapAnnotation
类并在创建注释时设置它(在调用 addAnnotation
之前)。
例如:
MapAnnotation *ann = [[MapAnnotation alloc] init];
ann.coordinate = ...
ann.title = ...
ann.subtitle = ...
ann.custAttr = custAttr; // <-- copy to the annotation object itself
[mapView addAnnotation:ann];
然后,在
viewForAnnotation
,阅读custAttr
来自 annotation
的属性(property)参数(在将其转换为 MapAnnotation *
之后)而不是引用外部声明的 custAttr
.您可能想为
custAttr
使用不同的名称。位于 MapAnnotation
的属性(property)以避免混淆。
关于iphone - 放大/缩小时 MKAnnotationView 错误更改了图钉图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13745211/