Pavel Kaljunen Pavel Kaljunen - 1 year ago 570
iOS Question

How do I remove all annotations from MKMapView except the user location annotation?

I use

to remove my annotations from
but same it remove user location ann. How can I prevent this, or how to get user ann back to view?

NSArray *annotationsOnMap = mapView.annotations;
[mapView removeAnnotations:annotationsOnMap];

Answer Source


When I tried with the iOS 9 SDK the user annotation is no longer removed.

Try this:

NSMutableArray * annotationsToRemove = [ mapView.annotations mutableCopy ] ;
[ annotationsToRemove removeObject:mapView.userLocation ] ;
[ mapView removeAnnotations:annotationsToRemove ] ;

EDIT: Swift version

let annotationsToRemove = mapView.annotations.filter { $0 !== mapView.userLocation }
mapView.removeAnnotations( annotationsToRemove )
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download