从mapView中删除用户位置注释

我必须删除添加到我的MKMapView的所有注释,但是当我执行时:

NSMutableArray *annotationsToRemove = [[NSMutableArray alloc] initWithArray: mapView.annotations]; [mapView removeAnnotations: annotationsToRemove]; 

数组annotationsToRemove包含MKUserLocation注释,但不会删除它。

有没有办法重置地图? 我需要删除它的所有注释!

您只需将mapView的showsUserLocation属性设置为NO即可。

  mapView.showsUserLocation = NO; 

实际上你无法编辑MKUserLocation注释。 我的意思是你无法从地图注释的数组中删除它,因为它是MKMapViewread-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输出,您将看到从mapView.annotations数组中删除了MKUserLocation注释。

这是我遵循的简单方法。 我怎么也不确定还有其他方法可以做到这一点。 如果您找到任何其他解决方案,请发表评论。

在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; } }