【问题标题】:MapKit update annotation imageMapKit 更新标注图片
【发布时间】:2011-07-03 07:47:26
【问题描述】:

在异步请求完成并提供有关注释状态的信息后,我无法找到更新自定义 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 都不是我所期望的那样,因此,我无法修改注释以更改图像或更新注释视图。

任何想法都将不胜感激!

【问题讨论】:

    标签: cocoa-touch ios4 mapkit mkannotation mkannotationview


    【解决方案1】:

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

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

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

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

    请参阅此other related question 了解类似示例。

    【讨论】:

    • 谢谢!我会听从您的建议,稍后再报告。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-16
    • 1970-01-01
    • 1970-01-01
    • 2011-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多