首先,MKMapView 中只有用户位置。经过一些操作后,我调用方法:
[self mapView:self.mapView didAddAnnotationViews:self.pointersArray];
我的didAddAnnotationViews
方法:
-(void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views
{
if (views.count == 1) {
MKAnnotationView *annotationView = [views objectAtIndex:0];
id<MKAnnotation>mp = [annotationView annotation];
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance([mp coordinate], 500, 500);
[mapView setRegion:region animated:YES];
}
else {
[mapView addAnnotations:views];
}
}
在不使用缩放之前,应用程序不会崩溃。但是当缩放使用超过 10 次(大约)时,我会在这个[mapView addAnnotations:views];
或有时在return UIApplicationMain(argc, argv, nil, NSStringFromClass([BIDAppDelegate class]));
. 错误 - EXC_BAD_ACCESS
。有我的问题吗?
编辑
更改为[self.mapView setRegion:region animated:YES];
但现在我在主线程中有错误MKNormalizedPointForLayer EXC_BAD_ACCESS
。通常缩放工作正常,应用程序在使用缩放 7 次或更多次后崩溃。我的按钮操作:
- (void)showKantorsOnMap {
if (self.kantorsData.count != self.pointersArray.count) {
NSLog(@"need to wait more");
}
NSMutableArray *toRemove = [[NSMutableArray alloc] init];
for (id annotation in self.mapView.annotations)
if (annotation != self.mapView.userLocation)
[toRemove addObject:annotation];
[self.mapView removeAnnotations:toRemove];
[self.mapView addAnnotations:self.pointersArray];
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(self.mapView.userLocation.coordinate,6500, 6500);
[self.mapView setRegion:region animated:YES];
}
解决方案
问题出在称为递归的didAddAnnotationViews
方法中。[mapView addAnnotations:views];