gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:56:01 25 4
gpt4 key购买 nike

我有一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com