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

32,990

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 })
Share:
32,990
Pavel Kaljunen
Author by

Pavel Kaljunen

Updated on November 28, 2020

Comments

  • Pavel Kaljunen
    Pavel Kaljunen over 3 years

    I use removeAnnotations to remove my annotations from mapView 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
    Pavel Kaljunen almost 12 years
    Thanks! This is exactly what I needed!
  • nielsbot
    nielsbot about 11 years
    this doesn't answer the question. In fact this code as no effect--listRemoveAnnotations is empty when -removeAnimations is called.
  • Christian Pappenberger
    Christian Pappenberger over 9 years
    Thanks also from me :)
  • nielsbot
    nielsbot almost 9 years
    this removes all the annotations, which is not what OP wanted.
  • nielsbot
    nielsbot almost 9 years
    The behavior has changed.
  • shapeare
    shapeare over 8 years
    This answer is much more concise than the accepted one.
  • user1019042
    user1019042 almost 8 years
    I'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
    Lenny1357 over 7 years
    I 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
    nielsbot over 7 years
    you have to look and see what line is failed on
  • Lenny1357
    Lenny1357 over 7 years
    It's in the MapView.removeAnnotations line
  • nielsbot
    nielsbot over 7 years
    Do 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
    OMEESH SHARMA over 4 years
    Most efficient answer.