How do I remove all map annotations in swift 2
Asked Answered
T

5

38

I had working code to remove all map annotations with a button, but after my update to xcode 7 I am running into the error:

Type 'MKAnnotation' does not conform to protocol 'SequenceType'

if let annotations = (self.mapView.annotations as? MKAnnotation){
    for _annotation in annotations {
        if let annotation = _annotation as? MKAnnotation {
            self.mapView.removeAnnotation(annotation)
        }
    }
}
Tactual answered 29/9, 2015 at 17:21 Comment(0)
C
88

In Swift 2 annotations is declared as non optional array [MKAnnotation] so you can easily write

let allAnnotations = self.mapView.annotations
self.mapView.removeAnnotations(allAnnotations)

without any type casting.

Conceit answered 29/9, 2015 at 17:26 Comment(0)
E
22
self.mapView.removeAnnotations(self.mapView.annotations)

If you don't want remove user location.

self.mapView.annotations.forEach {
  if !($0 is MKUserLocation) {
    self.mapView.removeAnnotation($0)
  }
}

Note: Objective-C now have generics, it is no longer necessary cast the elements of 'annotations' array.

Eno answered 29/9, 2015 at 17:41 Comment(1)
I want to use your code but it says: unexpectedly found nil while... I know what that means but I have no idea which value is nil.Combust
T
6

SWIFT 5

If you don't want to remove user location mark:

let annotations = mapView.annotations.filter({ !($0 is MKUserLocation) })
mapView.removeAnnotations(annotations)
Tanto answered 30/5, 2020 at 10:9 Comment(0)
P
1

The issue is that there are two methods. One is removeAnnotation which takes an MKAnnotation object, and the other is removeAnnotations which takes an array of MKAnnotations, note the "s" at the end of one and not the other. Attempting to cast from [MKAnnotation], an array to MKAnnotation a single object or visa versa will crash the program. The line of code self.mapView.annotations creates an array. Thus, if you are using the method removeAnnotation, you need to index the array for a single object within the array, as seen below:

let previousAnnotations = self.mapView.annotations
if !previousAnnotations.isEmpty{
  self.mapView.removeAnnotation(previousAnnotations[0])
}

Thus, you can remove various annotations while keeping the users location. You should always test your array before attempting to remove objects from it, else there is the possibly getting an out of bounds or nil error.

Note: using the method removeAnnotations (with an s)removes all annotations. If you are getting a nil, that implies that you have an empty array. You can verify that by adding an else statement after the if, like so;

    else{print("empty array")}
Profitsharing answered 11/3, 2018 at 1:24 Comment(0)
C
0

SWIFT 5 and Xcode 14.3 Target iOS 16.1

@IBOutlet var mapViewX: MKMapView!// Outlet.

// Clean all map annotations.
for annotationX in mapViewX.annotations
{
    mapViewX.removeAnnotation(annotationX)
}

// Delete specific annotation.
for annotationX in mapViewX.annotations
{
    if annotationX.title == "Your title"{
        mapViewX.removeAnnotation(annotationX)
    }
}

// Clean route lines.
for overlayX in mapViewX.overlays
{
    mapViewX.removeOverlay(overlayX)
}
Calaboose answered 13/4, 2023 at 7:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.