Lap*_*nou 2 iphone objective-c mkmapview ios
我有一个mapview数百个注释.我在后台加载注释,当加载所有注释时,我调用方法:setAnnotations.
我有6种类型的注释,彼此不同的引脚.
它适用于第一次装载.但是如果我想刷新地图(因为我使用过滤器),则图像图像会发生变化.
我在这里看到了类似的问题,但似乎对我不起作用.
这里是我的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]];
}
//...
Run Code Online (Sandbox Code Playgroud)
在这里我调用加载和重新加载地图的方法:
- (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;
});
});
}
Run Code Online (Sandbox Code Playgroud)
我做错了什么?
任何建议和帮助都很感激.先感谢您 ;)
问题是地图视图将重复使用具有相同重用标识符的引脚.这就是你创建引脚的方式:
annotationView = [[MKAnnotationView alloc] initWithAnnotation:singleAnnotation
reuseIdentifier:@"singleAnnotationView"];
Run Code Online (Sandbox Code Playgroud)
随后,您设置图像.有两种可能的解决方案:
这是#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]];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1523 次 |
| 最近记录: |