views:

1709

answers:

5

Hi all, i would 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 wich way can i check (like a for loop on all the annotations) if the annotation is not the blue dot annotation??

thanks in advance..

+9  A: 

Hi,

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 :

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

Hope this helps,

Sam

deanWombourne
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
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
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
Sorry i have forgotten the "break;" if the check is valid, to jump out the for loop...
Mat
A: 

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
+4  A: 

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
+1  A: 

The last line is wrong!!

This is correct: [myMap removeAnnotations:toRemove];

jlballes
A: 

This totally rocks! I changed the last line to "toRemove" as well. Before I was trying to iterate over each one to remove the individually and was getting:

'NSGenericException', reason: '* Collection was mutated while being enumerated.

Now it's fixed. If I wanted to remove all annotations in one go, however, I would use this:

mapView removeAnnotations:mapView.annotations];

gonzobrains