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

29,301

Solution 1

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

Solution 2

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.

Solution 3

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];

Solution 4

shortest way to clean all annotations and preserving MKUserLocation class annotation

[self.mapView removeAnnotations:self.mapView.annotations];

Solution 5

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

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

        [map removeAnnotation:annotation];
    }
    }

i modified like this

Share:
29,301

Related videos on Youtube

Mat
Author by

Mat

(my about me is currently blank) click here to edit

Updated on July 09, 2022

Comments

  • Mat
    Mat almost 2 years

    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]]; 
           } 
        }
    
    • skinny123
      skinny123 over 13 years
      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?
    • chrism
      chrism over 11 years
      try reversing the interation. Obviously, removing one then means that your indices are changing. Remove from the back.
    • nielsbot
      nielsbot about 9 years
  • Mat
    Mat over 14 years
    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..:)
  • deanWombourne
    deanWombourne over 14 years
    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. S
  • Henrik Erlandsson
    Henrik Erlandsson over 13 years
    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.
  • Mat
    Mat over 13 years
    Sorry i have forgotten the "break;" if the check is valid, to jump out the for loop...
  • Dr4ke the b4dass
    Dr4ke the b4dass over 13 years
    Per jlballes, the last line should read [myMap removeAnnotations:toRemove];
  • deanWombourne
    deanWombourne over 13 years
    Steve N - you are absolutely correct. I've edited my answer :)
  • HelmiB
    HelmiB almost 13 years
    [map removeAnnotations:[map annotations]]; should do the trick
  • deanWombourne
    deanWombourne almost 13 years
    @tlikyu - if you wanted to remove all the annotations then yes, you're correct. However, the op wanted to remove all but one :)
  • Ankit Srivastava
    Ankit Srivastava over 12 years
    I am so amazed by the quality of code some people write here.. thanks.
  • nielsbot
    nielsbot almost 10 years
    this will remove the user location
  • Apps-n-Add-Ons
    Apps-n-Add-Ons over 6 years
    receives error messageType '[MGLAnnotation]?' does not conform to protocol 'Sequence'