【问题标题】:MKAnnotationView fault when zoom in/out changed the pin image放大/缩小时 MKAnnotationView 错误更改了图钉图像
【发布时间】:2012-12-06 14:01:35
【问题描述】:

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

我从 Web 服务获取位置并识别它们,我使用字符串 (CustAttr) 作为“T”或“P”。

问题是来自网络服务的最后一次调用是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;
}

我该如何解决这个问题?有没有一种方法可以阻止它在放大/缩小时调用此方法,或者是否有另一种方法可以让它像在同一图像中一样再次绘制它们?

【问题讨论】:

标签: iphone objective-c ios mkmapview mkannotationview


【解决方案1】:

custAttr 变量(您在委托方法之外设置)并不总是与调用 viewForAnnotation 委托方法的 annotation 同步。

委托方法不一定在addAnnotationaddAnnotations之后调用,如果地图需要在缩放或平移后再次显示注释视图,可以为每个注释调用多次。

当它再次被同一个注解调用时,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 中,从annotation 参数中读取custAttr 属性(在将其转换为MapAnnotation * 之后),而不是引用外部声明的custAttr

您可能希望在MapAnnotation 中为custAttr 属性使用不同的名称以避免混淆。

【讨论】:

  • 这就是我要找的东西,我应该把它放在对象里面,非常感谢你
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-09
  • 1970-01-01
  • 2014-06-10
相关资源
最近更新 更多