Skip to content

Instantly share code, notes, and snippets.

@minikin
Last active April 13, 2023 08:19
Show Gist options
  • Save minikin/46f5599b5d46445c4c59 to your computer and use it in GitHub Desktop.
Save minikin/46f5599b5d46445c4c59 to your computer and use it in GitHub Desktop.
Remove all annotation from mapView [MapBox]
/// Remove all annotation from mapView (MapBox)
func removeAllAnnotations() {
guard let annotations = mapView.annotations else { return print("Annotations Error") }
if annotations.count != 0 {
for annotation in annotations {
mapView.removeAnnotation(annotation)
}
} else {
return
}
}
@JeffSchroeder
Copy link

    if let annotations = mapView.annotations {
        mapView.removeAnnotations(annotations)
    }

@idmbit
Copy link

idmbit commented Apr 13, 2023

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)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment