除了用户位置注释之外,如何从MKMapView中删除所有注释?

Pav*_*nen 40 xcode mkmapview mkannotation userlocation ios

removeAnnotations用来删除我的注释,mapView但同样删除用户位置ann.我该如何防止这种情况,或者如何让用户回来查看?

NSArray *annotationsOnMap = mapView.annotations;
        [mapView removeAnnotations:annotationsOnMap];
Run Code Online (Sandbox Code Playgroud)

nie*_*bot 91

更新:

当我尝试使用iOS 9 SDK时,不再删除用户注释.你可以简单地使用

mapView.removeAnnotations(mapView.annotations)
Run Code Online (Sandbox Code Playgroud)

历史答案(适用于在iOS 9之前在iOS上运行的应用):

试试这个:

NSMutableArray * annotationsToRemove = [ mapView.annotations mutableCopy ] ;
[ annotationsToRemove removeObject:mapView.userLocation ] ;
[ mapView removeAnnotations:annotationsToRemove ] ;
Run Code Online (Sandbox Code Playgroud)

编辑:Swift版本

let annotationsToRemove = mapView.annotations.filter { $0 !== mapView.userLocation }
mapView.removeAnnotations( annotationsToRemove )
Run Code Online (Sandbox Code Playgroud)


Asw*_*ose 20

要清除地图中的所有注释:

[self.mapView removeAnnotations:[self.mapView annotations]];
Run Code Online (Sandbox Code Playgroud)

从Mapview中删除指定的注释

 for (id <MKAnnotation> annotation in self.mapView.annotations)
{
    if (![annotation isKindOfClass:[MKUserLocation class]])
    {
              [self.mapView removeAnnotation:annotation];   
    }

}
Run Code Online (Sandbox Code Playgroud)

希望这可以帮到你.


ras*_*spi 7

对于Swift,您可以简单地使用单线程:

mapView.removeAnnotations(mapView.annotations)
Run Code Online (Sandbox Code Playgroud)

编辑:正如nielsbot所提到的,除非你像这样设置它,否则它也将删除用户的位置注释:

mapView.showsUserLocation = true
Run Code Online (Sandbox Code Playgroud)