How to remove all annotations from MKMapView without removing the blue dot?
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
Related videos on Youtube
Comments
-
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 over 13 yearsHey 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 over 11 yearstry reversing the interation. Obviously, removing one then means that your indices are changing. Remove from the back.
-
nielsbot about 9 yearspossible duplicate of How do I remove all annotations from MKMapView except the user location annotation?
-
-
Mat over 14 yearshi 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 over 14 yearsThat'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 over 13 yearsMat, 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 over 13 yearsSorry i have forgotten the "break;" if the check is valid, to jump out the for loop...
-
Dr4ke the b4dass over 13 yearsPer jlballes, the last line should read [myMap removeAnnotations:toRemove];
-
deanWombourne over 13 yearsSteve N - you are absolutely correct. I've edited my answer :)
-
HelmiB almost 13 years[map removeAnnotations:[map annotations]]; should do the trick
-
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 over 12 yearsI am so amazed by the quality of code some people write here.. thanks.
-
nielsbot almost 10 yearsthis will remove the user location
-
Apps-n-Add-Ons over 6 yearsreceives error messageType '[MGLAnnotation]?' does not conform to protocol 'Sequence'