Mapbox v10 iOS How to remove Annotation?
i started writing a Project with Mapbox Support and added a Polyline.
Then i append a second Polyline to the array and both are shown up on the mapView.
But i cant figure out how to remove an Annotation.
let firstPolyLineCoordinates = [
CLLocationCoordinate2DMake(48, 8),
CLLocationCoordinate2DMake(49, 9)
]
let secondPolyLineCoordinates = [
CLLocationCoordinate2DMake(47, 8),
CLLocationCoordinate2DMake(49.5, 9)
]
// Create the line annotation.
var firstPolyLineAnnotation = PolylineAnnotation(lineCoordinates: firstPolyLineCoordinates)
firstPolyLineAnnotation.lineColor = StyleColor(.red)
firstPolyLineAnnotation.lineWidth = 2
var secondPolyLineAnnotation = PolylineAnnotation(lineCoordinates: secondPolyLineCoordinates)
secondPolyLineAnnotation.lineColor = StyleColor(.blue)
secondPolyLineAnnotation.lineWidth = 2
// Create the `PolylineAnnotationManager` which will be responsible for handling this annotation
let polyLineAnnnotationManager = mapView.annotations.makePolylineAnnotationManager()
// Add the annotation to the manager.
polyLineAnnnotationManager.annotations = [firstPolyLineAnnotation]
polyLineAnnnotationManager.annotations.append(secondPolyLineAnnotation)
The Mapbox Docu says:
To remove a single annotation from an annotation manager, remove it from the annotations array.
https://docs.mapbox.com/ios/maps/guides/annotations/annotations/#removing-annotations
but there is no polyLineAnnnotationManager.annotations.remove(name_of_annotation)
like the polyLineAnnnotationManager.annotations.append(secondPolyLineAnnotation)
there is only a polyLineAnnnotationManager.annotations.removeAll()
but i dont wont to remove both (or all polylines)...
is there any hint?
If you use the init(id:lineCoordinates:)
PolylineAnnotation initialiser you can set the id
. Then you can just use some standard Swift way to delete a particular element.
let myID = "bongo" // id of annotation for deletion.
if let idx = polyLineAnnnotationManager.annotations.firstIndex(where: { $0.id == myID }) {
polyLineAnnnotationManager.annotations.remove(at: idx)
}