Skip to content

Instantly share code, notes, and snippets.

@minikin
Last active April 13, 2023 08:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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
}
}
@gulzatique
Copy link

its no removing my annotations

@greenzeal
Copy link

greenzeal commented Aug 22, 2017

[self.mapContainer removeAnnotations:self.mapContainer.annotations]; in Objective C

in Swift will be similar

@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