I use removeAnnotations
to remove my annotations from mapView
but same it remove user location ann. How can I prevent this, or how to get user ann back to view?
NSArray *annotationsOnMap = mapView.annotations;
[mapView removeAnnotations:annotationsOnMap];
For Swift you can simply use a one-liner:
Edit: As nielsbot mentioned it will also remove the user's location annotation unless you have set it up like this:
In Swift 4.1:
Normally if you don't want to remove your MKUserLocation annotation you can simply run:
self.mapView.removeAnnotations(self.annotations)
.This method by default does not remove the MKUserLocation annotation from the
annotations
list.However if you need to filter out all annotations except the MKUserLocation (see
annotationsNoUserLocation
variable below) for any other reason, like centering on all the annotations but the MKUserLocation annotation you can use this simple extension below.Hi try this i got the solution from this code:
How about some
NSPredicate
filter?Life is always better with NSPredicate filter
To clear all the annotations from the map:
To remove specified annotations from Mapview
Hope this may help you.
If your user location is kind of class of
MKUserLocation
, useisKindOfClass
to avoid removing user location annotation.Else you can set a flag to recognize the kind of your annotations in
– mapView:viewForAnnotation:
.