How to delete all Annotations on a MKMapView
Asked Answered
R

10

108

Is there a simple way to delete all the annotations on a map without iterating through all the displayed annotations in Objective-c?

Risa answered 12/6, 2010 at 3:44 Comment(0)
I
255

Yes, here is how

[mapView removeAnnotations:mapView.annotations]

However the previous line of code will remove all map annotations "PINS" from the map, including the user location pin "Blue Pin". To remove all map annotations and keep the user location pin on the map, there are two possible ways to do that

Example 1, retain the user location annotation, remove all pins, add the user location pin back, but there is a flaw with this approach, it will cause the user location pin to blink on the map, due to removing the pin then adding it back

- (void)removeAllPinsButUserLocation1 
{
    id userLocation = [mapView userLocation];
    [mapView removeAnnotations:[mapView annotations]];

    if ( userLocation != nil ) {
        [mapView addAnnotation:userLocation]; // will cause user location pin to blink
    }
}

Example 2, I personally prefer to avoid removing the location user pin in the first place,

- (void)removeAllPinsButUserLocation2
{
    id userLocation = [mapView userLocation];
    NSMutableArray *pins = [[NSMutableArray alloc] initWithArray:[mapView annotations]];
    if ( userLocation != nil ) {
        [pins removeObject:userLocation]; // avoid removing user location off the map
    }

    [mapView removeAnnotations:pins];
    [pins release];
    pins = nil;
}
Inert answered 12/6, 2010 at 6:39 Comment(2)
does this remove the user location too? what if I want to remove all the annotations besides the user location?Risa
You do not need to save any reference to user location. Read my answer below for more info.Dich
T
35

Here is the simplest way to do that:

-(void)removeAllAnnotations
{
  //Get the current user location annotation.
  id userAnnotation=mapView.userLocation;

  //Remove all added annotations
  [mapView removeAnnotations:mapView.annotations]; 

  // Add the current user location annotation again.
  if(userAnnotation!=nil)
  [mapView addAnnotation:userAnnotation];
}
Thorwald answered 15/1, 2011 at 7:30 Comment(4)
Good answer, much faster than iterating through all of them, especially if you have more than a handful of annotations.Oilcan
Removing an annotation then adding it back, causes the pin to blink on the map. Might not a be a big deal for some apps, but it can be annoying for the user if you consistently updating the map with new annotations.Inert
For some reason, my userLocation annotation always disappears using this method. Victor Van Hee's solution works for me.Bimonthly
horrible solution, it blinksMascia
S
18

Here's how to remove all annotations except the user location, written out explicitly because I imagine I will come looking for this answer again:

NSMutableArray *locs = [[NSMutableArray alloc] init];
for (id <MKAnnotation> annot in [mapView annotations])
{
    if ( [annot isKindOfClass:[ MKUserLocation class]] ) {
    }
    else {
        [locs addObject:annot];
    }
}
[mapView removeAnnotations:locs];
[locs release];
locs = nil;
Selfrealization answered 29/8, 2010 at 7:2 Comment(2)
Thank you,This worked for me with copy and paste and removing [locs release] and changing mapView to _mapView. I was following a great tutorial for MKDirections here devfright.com/mkdirections-tutorial and wanted to remove the pin after getting directions. I added the code below the last line of that method to the 'clear route'methodVolny
update remove multiple - (IBAction)clearRoute:(UIBarButtonItem *)sender { self.destinationLabel.text = nil; self.distanceLabel.text = nil; self.steps.text = nil; [self.mapView removeOverlay:routeDetails.polyline]; NSMutableArray *locs = [[NSMutableArray alloc] init]; for (id <MKAnnotation> annot in [_mapView annotations]) { if ( [annot isKindOfClass:[ MKUserLocation class]] ) { } else { [locs addObject:annot]; } } [_mapView removeAnnotations:locs]; [_mapView removeOverlays:_mapView.overlays]; }Volny
B
13

This is very similar to Sandip's answer, except that it doesn't re-add the user location so the blue dot doesn't blink on and off again.

-(void)removeAllAnnotations
{
    id userAnnotation = self.mapView.userLocation;

    NSMutableArray *annotations = [NSMutableArray arrayWithArray:self.mapView.annotations];
    [annotations removeObject:userAnnotation];

    [self.mapView removeAnnotations:annotations];
}
Blowbyblow answered 20/2, 2012 at 23:14 Comment(0)
D
13

You do not need to save any reference to user location. All that is needed is:

[mapView removeAnnotations:mapView.annotations]; 

And as long as you have mapView.showsUserLocation set to YES, you will still have user location on the map. Settings this property to YES basically asks the map view to start updating and fetching user location, to to show it on the map. From the MKMapView.h comments:

// Set to YES to add the user location annotation to the map and start updating its location
Dich answered 15/4, 2014 at 19:54 Comment(1)
I ended up using this format: [self.mapView.removeAnnotations(mapView.annotations)]Lloyd
C
8

Swift 2.0 Simple and the best:

mapView.removeAnnotations(mapView.annotations)
Cryoscope answered 18/3, 2016 at 12:51 Comment(0)
H
6

Swift version:

func removeAllAnnotations() {
    let annotations = mapView.annotations.filter {
        $0 !== self.mapView.userLocation
    }
    mapView.removeAnnotations(annotations)
}
Homestead answered 2/8, 2015 at 4:7 Comment(0)
J
5

Swift 3

if let annotations = self.mapView.annotations {
    self.mapView.removeAnnotations(annotations)
}
Jael answered 10/10, 2016 at 19:26 Comment(0)
W
0

To remove one type of subclass you can do

mapView.removeAnnotations(mapView.annotations.filter({$0 is PlacesAnnotation}))

where PlacesAnnotation is a subclass of MKAnnotation

Windproof answered 3/7, 2019 at 8:56 Comment(0)
C
0

Here is the function to remove all markers as well as all routes (if any) from MKMapView:

 func removeAppleMapOverlays() {
    let overlays = self.appleMapView.overlays
    self.appleMapView.removeOverlays(overlays)
    let annotations = self.appleMapView.annotations.filter {
            $0 !== self.appleMapView.userLocation
        }
    self.appleMapView.removeAnnotations(annotations)
}

Cheers

Ceroplastics answered 24/11, 2020 at 18:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.