ios - 刷新 map 后错误的图像 MKAnnotationView

标签 ios iphone objective-c mkmapview

我有一个 mapview 数百个注释。我在后台加载注释,当所有注释都加载后,我调用方法:setAnnotations。

我有 6 种类型的注释,它们的引脚各不相同。

第一次加载效果很好。但是如果我想刷新 map (因为我使用了过滤器),图钉图像就会改变。

我看到了类似的问题 Here ,但它似乎对我不起作用。

这里是我的 viewForAnnotation 方法:

//... 
else if ([annotation isKindOfClass:[CustomAnnotation class]]) {

        CustomAnnotation *singleAnnotation = (CustomAnnotation *)annotation;
        annotationView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"singleAnnotationView"];

        if (!annotationView) {
            annotationView = [[MKAnnotationView alloc] initWithAnnotation:singleAnnotation reuseIdentifier:@"singleAnnotationView"];
            annotationView.canShowCallout = YES;
            annotationView.centerOffset = CGPointMake(0, -20);
            annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        }
        else
            annotationView.annotation = annotation;

// Solution, put it here:
annotationView.image = [UIImage imageNamed:[NSString stringWithFormat:@"pin_%d", singleAnnotation.idType]];
    } 
//...

在这里我调用了我的方法来加载和重新加载 map :

- (void)loadMapView
{
    UIActivityIndicatorView *a = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    self.mapview.hidden = YES;
    a.center = CGPointMake(160, 240);
    [self.view addSubview:a];
    [a startAnimating];

    NSMutableArray *array = [NSMutableArray new];

    if(self.mapview.annotations.count > 0){
        [self.mapview removeAnnotations:self.mapview.annotations];
        [self.mapview removeOverlays:self.mapview.overlays];
    }

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{

        for(MapObjects *obj in [MapRepository shared].repository){

            CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(obj.coord_geo_latitude,obj.coord_geo_longitude);

            CustomAnnotation *annotation = [[CustomAnnotation alloc] initWithCoordinates:coordinate
                                                                               placeName:obj.placeName
                                                                             description:obj.description
idType:obj.idType];

            if(annotation.coordinate.latitude != 0 || annotation.coordinate.longitude != 0)
                [array addObject:annotation];
        }

        dispatch_async(dispatch_get_main_queue(), ^{

            [self.mapview addAnnotations:array];
            [self.mapview zoomToFitMapAnnotations:self.mapview];
            [a stopAnimating];
            self.mapview.hidden = NO;
        });
    });  
}

我做错了什么?

如有任何建议和帮助,我们将不胜感激。 提前谢谢你;)

最佳答案

问题在于 map View 将重复使用具有相同重用标识符的图钉。这就是您创建图钉的方式:

annotationView = [[MKAnnotationView alloc] initWithAnnotation:singleAnnotation
                                              reuseIdentifier:@"singleAnnotationView"];

随后,您设置图像。有两种可能的解决方案:

  1. 为每种图像类型使用不同的重用标识符。这样,这些引脚将根据需要进行缓存。
  2. 在您创建图片后将其设置在图钉上。这样,所有引脚都进入同一个重用池并根据需要进行自定义。

这是 #2 的样子:

CustomAnnotation *singleAnnotation = (CustomAnnotation *)annotation;
annotationView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"singleAnnotationView"];

if (!annotationView) {
    annotationView = [[MKAnnotationView alloc] initWithAnnotation:singleAnnotation reuseIdentifier:@"singleAnnotationView"];
    annotationView.canShowCallout = YES;
    annotationView.centerOffset = CGPointMake(0, -20);
    annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
}
else {
    annotationView.annotation = annotation;
}

annotationView.image = [UIImage imageNamed:[NSString stringWithFormat:@"pin_%d", singleAnnotation.idType]];

关于ios - 刷新 map 后错误的图像 MKAnnotationView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20857562/

相关文章:

ios - viewDidLoad 中的文本消息窗口在其他所有内容之后弹出

iphone - 将音频文件保存到 NSURL

objective-c - 调整 NSWindow 的大小以适应新的 NSViews 会导致 View 绘制不正确

ios - 为什么这不足以添加一个 View 以使用自动布局占据所有屏幕?

ios - Swift - 无法通过导航 Controller 中的自定义选项卡寻呼机传递数据

ios - 传递数据 - Swift

ios - Flutter (iOS) 数字键盘小数点分隔符

iphone - iPhone 设备的连接

objective-c - 以编程方式查找 Dock 的位置?

iOS 应用程序在 Facebook 登录时启动另一个应用程序