Skip to content

Commit 3283953

Browse files
Jill Cardamonkried
authored andcommitted
Make destination annotation methods generic and change identifiers.
1 parent 5719062 commit 3283953

File tree

1 file changed

+31
-8
lines changed

1 file changed

+31
-8
lines changed

Sources/MapboxNavigation/NavigationMapView.swift

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1335,14 +1335,30 @@ open class NavigationMapView: UIView {
13351335

13361336
if let lastLeg = route.legs.last,
13371337
let destinationCoordinate = lastLeg.destination?.coordinate {
1338-
addDestinationAnnotation(destinationCoordinate)
1338+
addDestinationAnnotation(destinationCoordinate) { [weak self] in
1339+
guard self != nil else { return }
1340+
}
13391341
}
13401342
}
13411343

1342-
func addDestinationAnnotation(_ coordinate: CLLocationCoordinate2D,
1343-
identifier: String = NavigationMapView.AnnotationIdentifier.finalDestinationAnnotation) {
1344+
/**
1345+
Adds a final destination annotation to the map.
1346+
1347+
- parameter coordinate: Coordinate which represents the annotation location.
1348+
- parameter identifier: String to uniquely identify the destination annotation. Defaults to `nil` and a default identifier will be provided.
1349+
- parameter styleLoaded: An escaping closure to be executed when the `MapView` style has finished loading.
1350+
*/
1351+
public func addDestinationAnnotation(_ coordinate: CLLocationCoordinate2D,
1352+
identifier: String? = nil,
1353+
styleLoaded: @escaping () -> Void) {
1354+
let identifier = identifier ?? String("finalDestinationAnnotation_\(finalDestinationAnnotations.count)")
13441355
var destinationAnnotation = PointAnnotation(id: identifier, coordinate: coordinate)
13451356
destinationAnnotation.image = .init(image: .defaultMarkerImage, name: ImageIdentifier.markerImage)
1357+
1358+
mapView.mapboxMap.onNext(event: .styleLoaded) { [weak self] _ in
1359+
guard self != nil else { return }
1360+
styleLoaded()
1361+
}
13461362

13471363
// If `PointAnnotationManager` is available - add `PointAnnotation`, if not - remember it
13481364
// and add it only after fully loading `MapView` style.
@@ -1356,12 +1372,19 @@ open class NavigationMapView: UIView {
13561372
}
13571373
}
13581374

1359-
func removeDestinationAnnotation(_ identifier: String = NavigationMapView.AnnotationIdentifier.finalDestinationAnnotation) {
1360-
let remainingAnnotations = pointAnnotationManager?.annotations.filter {
1361-
$0.id != identifier
1375+
/**
1376+
Removes a final destination annotation to the map.
1377+
1378+
- parameter identifier: String to uniquely identify the destination annotation to be removed. Defaults to `nil` and removes all destination annotations.
1379+
*/
1380+
public func removeDestinationAnnotation(_ identifier: String? = nil) {
1381+
if let identifier {
1382+
finalDestinationAnnotations.removeAll(where: { $0.id == identifier })
1383+
pointAnnotationManager?.annotations.removeAll(where: { $0.id == identifier })
1384+
} else {
1385+
finalDestinationAnnotations.removeAll(where: { $0.id.contains("finalDestinationAnnotation") })
1386+
pointAnnotationManager?.annotations.removeAll(where: { $0.id.contains("finalDestinationAnnotation") })
13621387
}
1363-
1364-
pointAnnotationManager?.annotations = remainingAnnotations ?? []
13651388
}
13661389

13671390
/**

0 commit comments

Comments
 (0)