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]]; 
       } 
    }
Mat
  • 7,613
  • 4
  • 40
  • 56
  • 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? – skinny123 Jan 18 '11 at 15:39
  • try reversing the interation. Obviously, removing one then means that your indices are changing. Remove from the back. – chrism Oct 18 '12 at 21:31
  • possible duplicate of [How do I remove all annotations from MKMapView except the user location annotation?](http://stackoverflow.com/questions/10865088/how-do-i-remove-all-annotations-from-mkmapview-except-the-user-location-annotati) – nielsbot Feb 18 '15 at 04:44

7 Answers7

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

deanWombourne
  • 38,189
  • 13
  • 98
  • 110
  • 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..:) – Mat Jan 25 '10 at 12:44
  • 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 – deanWombourne Jan 25 '10 at 12:47
  • 1
    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. – Henrik Erlandsson Aug 23 '10 at 13:43
  • Sorry i have forgotten the "break;" if the check is valid, to jump out the for loop... – Mat Sep 15 '10 at 16:53
  • 1
    Per jlballes, the last line should read [myMap removeAnnotations:toRemove]; – Steve N Dec 13 '10 at 22:58
  • Steve N - you are absolutely correct. I've edited my answer :) – deanWombourne Dec 14 '10 at 09:59
  • 1
    [map removeAnnotations:[map annotations]]; should do the trick – HelmiB Jun 23 '11 at 02:49
  • @tlikyu - if you wanted to remove all the annotations then yes, you're correct. However, the op wanted to remove all but one :) – deanWombourne Jun 23 '11 at 09:48
  • I am so amazed by the quality of code some people write here.. thanks. – Ankit Srivastava Nov 24 '11 at 10:05
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.

Sebastian Bean
  • 142
  • 2
  • 8
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];
Mikael
  • 5,429
  • 5
  • 30
  • 38
8

shortest way to clean all annotations and preserving MKUserLocation class annotation

[self.mapView removeAnnotations:self.mapView.annotations];
DazChong
  • 3,513
  • 27
  • 23
6
for (id annotation in map.annotations) {
    NSLog(@"annotation %@", annotation);

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

        [map removeAnnotation:annotation];
    }
    }

i modified like this

chings228
  • 1,859
  • 24
  • 24
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];
Jahm
  • 658
  • 1
  • 12
  • 27
kunalg
  • 1,570
  • 1
  • 12
  • 18
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)
    }
}
Laszlo
  • 2,803
  • 2
  • 28
  • 33