我有一个mapview数百个注释。我在后台加载注释,当加载所有注释时,我调用方法:setAnnotations。
我有6种类型的注释,彼此不同的引脚。
适用于第一次加载。但是如果我想刷新地图(因为我使用过滤器),则图像图像会发生变化。
我看到了类似的问题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]];
}
//...
这里我调用的方法是加载并重新加载地图:
- (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;
});
});
}
我做错了什么?
任何建议和帮助都表示赞赏。 提前谢谢你;)
答案 0 :(得分:9)
问题是地图视图将重复使用具有相同重用标识符的引脚。这就是你创建引脚的方式:
annotationView = [[MKAnnotationView alloc] initWithAnnotation:singleAnnotation
reuseIdentifier:@"singleAnnotationView"];
随后,您设置图像。有两种可能的解决方案:
这是#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]];
答案 1 :(得分:1)
你使用可重用性(类似于tableView行)你的图像为1..12..24 ..和下一个将是相同的。如果要自定义图像,则必须在可重用性if
之外插入代码。
我的意思是这一行不应该在if (!annotationView)
annotationView.image = [UIImage imageNamed:[NSString stringWithFormat:@"pin_%d", singleAnnotation.idType]];