How do I remove all annotations from MKMapView except the user location annotation?
Solution 1
Update:
When I tried with the iOS 9 SDK the user annotation is no longer removed. You can simply use
mapView.removeAnnotations(mapView.annotations)
Historical answer (for apps that run on iOS before iOS 9):
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 )
Solution 2
To clear all the annotations from the map:
[self.mapView removeAnnotations:[self.mapView annotations]];
To remove specified annotations from Mapview
for (id <MKAnnotation> annotation in self.mapView.annotations)
{
if (![annotation isKindOfClass:[MKUserLocation class]])
{
[self.mapView removeAnnotation:annotation];
}
}
Hope this may help you.
Solution 3
For Swift you can simply use a one-liner:
mapView.removeAnnotations(mapView.annotations)
Edit: As nielsbot mentioned it will also remove the user's location annotation unless you have set it up like this:
mapView.showsUserLocation = true
Solution 4
If your user location is kind of class of MKUserLocation
, use isKindOfClass
to avoid removing user location annotation.
if (![annotation isKindOfClass:[MKUserLocation class]]) {
}
Else you can set a flag to recognize the kind of your annotations in – mapView:viewForAnnotation:
.
Solution 5
Swift 4.2 or later
Add this line before adding the annotations
mapView.removeAnnotations(mapView.annotations.filter { $0 !== mapView.userLocation })
Pavel Kaljunen
Updated on November 28, 2020Comments
-
Pavel Kaljunen over 3 years
I use
removeAnnotations
to remove my annotations frommapView
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];
-
Pavel Kaljunen almost 12 yearsThanks! This is exactly what I needed!
-
nielsbot about 11 yearsthis doesn't answer the question. In fact this code as no effect--
listRemoveAnnotations
is empty when-removeAnimations
is called. -
Christian Pappenberger over 9 yearsThanks also from me :)
-
nielsbot almost 9 yearsthis removes all the annotations, which is not what OP wanted.
-
nielsbot almost 9 yearsThe behavior has changed.
-
shapeare over 8 yearsThis answer is much more concise than the accepted one.
-
user1019042 almost 8 yearsI've tried using this instead: self.mapView.viewForAnnotation(annotation!)?.hidden = true but it was giving me weird errors when I change map region. Yours is the better way, thank you!
-
Lenny1357 over 7 yearsI implemented this code but I get a fatal error: unexpectedly found nil. I checked whether annotationsToRemove counts greater than one. I have no idea why I am getting this error. Please help me.
-
nielsbot over 7 yearsyou have to look and see what line is failed on
-
Lenny1357 over 7 yearsIt's in the MapView.removeAnnotations line
-
nielsbot over 7 yearsDo you have any
!
in your code? How is your MapView variable defined? Change any!
to?
and then further modify your code so it compiles. The crash will probably be resolved. -
OMEESH SHARMA over 4 yearsMost efficient answer.