cocoa-touch - MapKit更新标注图片

标签 cocoa-touch ios4 mapkit mkannotation mkannotationview

在异步请求完成注释状态信息后,我无法找到更新自定义 MKAnnotationView 图像的方法。到目前为止,我有这个:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {

    static NSString *identifier = @"EstacionEB";   
    if ([annotation isKindOfClass:[EstacionEB class]]) {
        EstacionEB *location = (EstacionEB *) annotation;

        CustomPin *annotationView = (CustomPin *) [_mapita dequeueReusableAnnotationViewWithIdentifier:identifier];
        if (annotationView == nil) {
            annotationView = [[CustomPin alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
        } else {
            annotationView.annotation = annotation;
        }

        UIImage * image = [UIImage imageNamed:[NSString stringWithFormat:@"%@.png", [location elStatus]]];

        annotationView.enabled = YES;
        annotationView.canShowCallout = YES;
        annotationView.image = image;

        NSDictionary *temp = [[NSDictionary alloc] 
                              initWithObjects:[NSArray arrayWithObjects:annotationView, location, nil]
                              forKeys:[NSArray arrayWithObjects:@"view", @"annotation", nil]
                              ];
        //This array is synthesized and inited in my controller's viewDidLoad
        [self.markers setObject:temp forKey:location.eid];
        return annotationView;
    }

    return nil;    
}

不久之后,我做了一个请求,结果是一个 NSDictionary,我正在尝试执行以下操作,它向两个元素返回 null:

- (void)updateStation:(NSString *)eid withDetails:(NSDictionary *)details
{
    NSInteger free = [details objectForKey:@"free"];
    NSInteger parkings = [details objectForKey:@"parkings"];

    NSDictionary *storedStations = [self.markers objectForKey:eid];

    CustomPin *pin = [storedStations objectForKey:@"view"]; //nil
    EstacionEB *station = [referencia objectForKey:@"annotation"]; //nil as well

    [station setSubtitle:free];

    NSString *status;
    if( free==0 ){
        status = @"empty";
    } else if( (free.intValue>0) && (parkings.intValue<=3)  ){
        status = @"warning";
    } else {
        status = @"available";
    }
    UIImage * image = [UIImage imageNamed:[NSString imageWithFormat:@"%@.png", status]];
    pin.image = image;
}

这不会带来任何错误(假设我正确地粘贴和翻译了所有内容),但是 NSMutableDictionary 应该同时包含我的自定义 MKAnnotationView 和 MKAnnotation,但即使我在请求完成之前将它们全部记录下来并且它正确显示,当请求完成,就好像 MKAnnotationView 和 MKAnnotation 都不是我所期望的,因此,我无法修改注释来更改图像或更新注释 View 。

如有任何想法,我们将不胜感激!

最佳答案

我不确定您为什么从标记数组中获取 nil 值(尤其是注释)。但是,我不建议像那样存储对注释 View 的引用。

viewForAnnotation 委托(delegate)方法可以在它认为有必要的任何时候被 map View 调用,并且 View 对象可以从一次调用更改为下一次调用。由于您还为每个注释使用相同的重用标识符来重用注释 View ,因此也有可能稍后将相同的 View 对象重新用于另一个注释。

相反,在 updateStation 中,我建议如下:

  • 遍历 map View 的annotations数组
  • 如果注解的类型是EstacionEB,则检查它的eid是否与正在更新的相匹配
  • 更新注释的 subTitleelStatus 属性(更新 elStatus 很重要,因为它被 viewForAnnotation 使用委托(delegate)方法来设置图像)
  • 通过调用 map View 的 viewForAnnotation: 实例方法获取注释的当前 View (这与委托(delegate)方法相同 mapView:viewForAnnotation:)
  • 更新 View 的image属性

查看此 other related question对于类似的例子。

关于cocoa-touch - MapKit更新标注图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6561722/

相关文章:

iphone - 如何在IB中使用UIScrollView?

iphone - 如何检测哪个 UILabel 被点击了?

iphone - 如何在 Mapkit 上启用完全水平滚动?

ios - 更改 MKMapView 的缩放 - Swift 4

objective-c - 按下按钮时应用程序崩溃

objective-c - 我该如何做NSPredicate代码?

iphone - 核心数据和核心位置

iphone - MFMailComposeViewController 无法将 URL 识别为链接

iphone - 设置 uitableviewcellaccessory 的位置

objective-c - MKMapView 设置区域 : crashes at edge of world