我必须删除添加到我的 MKMapView 中的所有注释,但是当我执行时:
NSMutableArray *annotationsToRemove = [[NSMutableArray alloc] initWithArray: mapView.annotations];
[mapView removeAnnotations: annotationsToRemove];
数组注释ToRemove 包含一个MKUserLocation
注释,并且不会删除它。
有没有办法重置地图?我需要从中删除所有注释!
您可以将 mapView 的 showUserLocation 属性设置为 NO。
mapView.showsUserLocation = NO;
实际上您无法编辑MKUserLocation
注释。我的意思是你不能从地图注释的数组中删除它,因为它是MKMapView
的read-only
属性。
如果您访问MKMapView.h
课程。您将在下面找到一行
@property (nonatomic, readonly) MKUserLocation *userLocation;
在这里我们可以看到此属性是只读的。所以我们不能从注释数组中删除它MKMapView
。您在使用其他注释进行计算时遇到困难时,都可以在运行时隐藏它。
我想解释的是,当不再需要用户位置时,您可以为用户位置属性设置NO
。
例如,使用您的代码:
NSMutableArray *annotationsToRemove = [[NSMutableArray alloc] initWithArray: mapView.annotations];
[mapView removeAnnotations: annotationsToRemove];
[self.mapView setShowsUserLocation:NO];
NSLog(@"MapView annotations :%@", mapView.annotations);
检查输出NSLog
,您将看到MKUserLocation
注释已从数组中删除mapView.annotations
。
这是我遵循的简单方法。我怎么不确定还有其他方法可以做到这一点。如果您找到任何其他解决方案,请发表评论。
在 h 插入
@property (weak) MKAnnotationView *ulv;
在 m 插入
- (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated {
MKZoomScale currentZoomScale = mapView.bounds.size.width / mapView.visibleMapRect.size.width;
NSLog(@"current zoom scale is %f",currentZoomScale);
ulv = [mapView viewForAnnotation:mapView.userLocation];
if( currentZoomScale > 0.049 ){
ulv.hidden = YES;
}else{
ulv.hidden = NO;
}
}