MapKit更新注释图像 [英] MapKit update annotation image

查看:70
本文介绍了MapKit更新注释图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在异步请求完成后,我发现更新自定义 MKAnnotationView 图像的方法时出现问题,并提供有关注释状态的信息。到目前为止,我有这个:

I'm having a problem finding out the way to update a custom MKAnnotationView image after a asynchronous request completes with information about the status of the annotation. So far, I have this:

- (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:

A little after, I do a request and with the results, an NSDictionary, I'm trying to do the following, which returns null to both elements:

- (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都不是我期望的那样,因此,我无法修改注释来更改图像或更新注释视图。

This brings up no errors (assuming I pasted and traduced everything correctly), but the NSMutableDictionary that should contain both my custom MKAnnotationView and the MKAnnotation, but even when I log them all before the request completes and it appears correctly, when the request completes its as if both the MKAnnotationView and MKAnnotation are just not what I expected, and thus, I can't modify the annotations to change the image or update the annotation view.

任何想法都将不胜感激!

Any ideas would be appreciated!

推荐答案

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

I'm not sure why you're getting nil values from your markers array (especially for annotation). However, I don't recommend storing a reference to the annotation view like that.

viewForAnnotation 委托方法可以在它认为必要时由地图视图调用,并且视图对象可以从一个电话改为下一个电话。由于您还使用相同的重复使用标识符为每个注释重新使用注释视图,因此稍后可能会将相同的视图对象重新用于另一个注释。

The viewForAnnotation delegate method can get called by the map view any time it thinks it's necessary and the view object could change from one call to the next. Since you are also re-using annotation views using the same re-use identifier for each annotation, there's also the possibility that the same view object could be re-used for another annotation later.

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

Instead, in updateStation, I suggest the following:


  • loop通过地图视图的注释数组

  • 如果注释的类型为 EstacionEB ,然后检查其 eid 是否与正在更新的那个匹配

  • 更新注释的 subTitle elStatus 属性(更新 elStatus 非常重要,因为它被 viewForAnnotation <使用/ code>设置图像的委托方法)

  • 通过调用地图视图的 viewForAnnotation:实例来获取注释的当前视图方法( 与委托方法 mapView:viewForAnnotation:相同)

  • 更新视图的图像属性
  • loop through the map view's annotations array
  • if the annotation is of type EstacionEB, then check if its eid matches the one being updated
  • update the annotation's subTitle and elStatus properties (it's important to update elStatus because it's used by the viewForAnnotation delegate method to set the image)
  • get the annotation's current view by calling the map view's viewForAnnotation: instance method (this is not the same as the delegate method mapView:viewForAnnotation:)
  • update the image property of the view

查看此类似示例的其他相关问题

这篇关于MapKit更新注释图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆