iphone - 放大/缩小时 MKAnnotationView 错误更改了图钉图像

标签 iphone objective-c ios mkmapview mkannotationview

我使用了注释图并为图钉使用了不止一张图像,但是每当我放大或缩小时,它会将所有图钉更改为一张图像。

我从 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/

相关文章:

iphone - 在 iPhone 上完成聊天的不同方式

iphone - iphone/ipod 的序列号

ios - NS编码: How to create required init in inherited classes

iPhone RestKit 如何加载本地 JSON 文件并将其映射到核心数据实体?

css - Safari - 过渡在 Safari(手机版)中不起作用

ios - 在核心数据中加载图像时 UITableView 滞后

iOS Modal Segue 放弃源 ViewController?

javascript - 如何从 wkWebview/webView 中的 Objective-C 更改 `&lt;input&gt;` 元素属性(例如值)

android - 如何在代号一中获得频繁的后台 GPS 更新?

iOS 可达性不起作用