How to remove all annotations from MKMapView without removing the blue dot?
Asked Answered
P

7

26

I would like to remove all annotations from my mapview without the blue dot of my position. When I call:

[mapView removeAnnotations:mapView.annotations];

all annotations are removed.

In which way can I check (like a for loop on all the annotations) if the annotation is not the blue dot annotation?

EDIT (I've solved with this):

for (int i =0; i < [mapView.annotations count]; i++) { 
    if ([[mapView.annotations objectAtIndex:i] isKindOfClass:[MyAnnotationClass class]]) {                      
         [mapView removeAnnotation:[mapView.annotations objectAtIndex:i]]; 
       } 
    }
Pruitt answered 25/1, 2010 at 12:26 Comment(3)
Hey Mat, I tried using your code, and it works, though for some reason instead of removing one pin at a time it gets rid of 3 or 2 at a time....what's up with that?Mariannmarianna
try reversing the interation. Obviously, removing one then means that your indices are changing. Remove from the back.Fiona
possible duplicate of How do I remove all annotations from MKMapView except the user location annotation?Kurr
L
58

Looking at the MKMapView documentation, it seems like you have the annotations property to play with. It should be pretty simple to iterate through this and see what annotations you have :

for (id annotation in myMap.annotations) {
    NSLog(@"%@", annotation);
}

You also have the userLocation property which gives you the annotation representing the user's location. If you go through the annotations and remember all of them which are not the user location, you can then remove them using the removeAnnotations: method :

NSInteger toRemoveCount = myMap.annotations.count;
NSMutableArray *toRemove = [NSMutableArray arrayWithCapacity:toRemoveCount];
for (id annotation in myMap.annotations)
    if (annotation != myMap.userLocation)
        [toRemove addObject:annotation];
[myMap removeAnnotations:toRemove];

Hope this helps,

Sam

Lavallee answered 25/1, 2010 at 12:38 Comment(9)
hi thank you for your trick Sam!, i've also now solved in this way: for (int i =0; i < [mapView.annotations count]; i++) { if ([[mapView.annotations objectAtIndex:i] isKindOfClass:[MyAnnotationClass class]]) { [mapView removeAnnotation:[mapView.annotations objectAtIndex:i]]; } } ..thanks again..:)Pruitt
That's a pretty good way of doing it too - I'm removing all annotations but you're removing only the annotations that you've added which is probably a safer thing to do. Nice one. SLavallee
Mat, just a thought: won't that skip an annotation that is moved in place of one that is removed? Seems the annotation after would get the index that the removed one had, but the i counter mercilessly increases.Sinkage
Sorry i have forgotten the "break;" if the check is valid, to jump out the for loop...Pruitt
Per jlballes, the last line should read [myMap removeAnnotations:toRemove];Kiley
Steve N - you are absolutely correct. I've edited my answer :)Lavallee
[map removeAnnotations:[map annotations]]; should do the trickAvouch
@tlikyu - if you wanted to remove all the annotations then yes, you're correct. However, the op wanted to remove all but one :)Lavallee
I am so amazed by the quality of code some people write here.. thanks.Veery
F
31

If you like quick and simple, there's a way to filter an array of the MKUserLocation annotation. You can pass this into MKMapView's removeAnnotations: function.

 [_mapView.annotations filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"!(self isKindOfClass: %@)", [MKUserLocation class]]];

I assume this is pretty much the same as the manual filters posted above, except using a predicate to do the dirty work.

Fatalism answered 25/1, 2010 at 12:27 Comment(0)
C
13

Isn't it easier to just do the following:

//copy your annotations to an array
    NSMutableArray *annotationsToRemove = [[NSMutableArray alloc] initWithArray: mapView.annotations]; 
//Remove the object userlocation
    [annotationsToRemove removeObject: mapView.userLocation]; 
 //Remove all annotations in the array from the mapView
    [mapView removeAnnotations: annotationsToRemove];
    [annotationsToRemove release];
Caspian answered 22/3, 2012 at 17:28 Comment(0)
G
8

shortest way to clean all annotations and preserving MKUserLocation class annotation

[self.mapView removeAnnotations:self.mapView.annotations];
Guildsman answered 20/9, 2013 at 8:58 Comment(1)
this will remove the user locationKurr
H
6
for (id annotation in map.annotations) {
    NSLog(@"annotation %@", annotation);

    if (![annotation isKindOfClass:[MKUserLocation class]]){

        [map removeAnnotation:annotation];
    }
    }

i modified like this

Hendrick answered 16/8, 2011 at 6:5 Comment(0)
A
1

it easier to just do the following:

NSMutableArray *annotationsToRemove = [NSMutableArray arrayWithCapacity:[self.mapView.annotations count]];
    for (int i = 1; i < [self.mapView.annotations count]; i++) {
        if ([[self.mapView.annotations objectAtIndex:i] isKindOfClass:[AddressAnnotation class]]) {
            [annotationsToRemove addObject:[self.mapView.annotations objectAtIndex:i]];
            [self.mapView removeAnnotations:annotationsToRemove];
        }
    }

[self.mapView removeAnnotations:annotationsToRemove];
Arouse answered 13/12, 2012 at 13:23 Comment(0)
Z
0

For Swift 3.0

for annotation in self.mapView.annotations {
    if let _ = annotation as? MKUserLocation {
       // keep the user location
    } else {
       self.mapView.removeAnnotation(annotation)
    }
}
Zhao answered 17/4, 2017 at 12:42 Comment(1)
receives error messageType '[MGLAnnotation]?' does not conform to protocol 'Sequence'Berte

© 2022 - 2024 — McMap. All rights reserved.