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.
extension MKMapView {
var annotationsNoUserLocation : [MKAnnotation] {
get {
return self.annotations.filter{ !($0 is MKUserLocation) }
}
}
func showAllAnnotations() {
self.showAnnotations(self.annotations, animated: true)
}
func removeAllAnnotations() {
self.removeAnnotations(self.annotations)
}
func showAllAnnotationsNoUserLocation() {
self.showAnnotations(self.annotationsNoUserLocation, animated: true)
}
}