Skip to content

Commit c6031f1

Browse files
committed
Add onUserTrackingModeChanged modifier to MapView.
1 parent 98aa8cd commit c6031f1

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

Sources/MapLibreSwiftUI/MapView.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public struct MapView<T: MapViewHostViewController>: UIViewControllerRepresentab
3333
var gestures = [MapGesture]()
3434

3535
var onStyleLoaded: ((MLNStyle) -> Void)?
36+
var onUserTrackingModeChanged: ((MLNUserTrackingMode, Bool) -> Void)?
3637
var onViewProxyChanged: ((MapViewProxy) -> Void)?
3738
var proxyUpdateMode: ProxyUpdateMode?
3839

@@ -105,6 +106,9 @@ public struct MapView<T: MapViewHostViewController>: UIViewControllerRepresentab
105106
// Link the style loaded to the coordinator that emits the delegate event.
106107
context.coordinator.onStyleLoaded = onStyleLoaded
107108

109+
// Link the user tracking change to the coordinator that emits the delegate event.
110+
context.coordinator.onUserTrackingModeChange = onUserTrackingModeChanged
111+
108112
// Add all gesture recognizers
109113
for gesture in gestures {
110114
registerGesture(controller.mapView, context, gesture: gesture)

Sources/MapLibreSwiftUI/MapViewCoordinator.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ MLNMapViewDelegate {
4444
var cameraUpdateContinuation: CheckedContinuation<Void, Never>?
4545

4646
var onStyleLoaded: ((MLNStyle) -> Void)?
47+
var onUserTrackingModeChange: ((MLNUserTrackingMode, Bool) -> Void)?
4748
var onGesture: (MLNMapView, UIGestureRecognizer) -> Void
4849
var onViewProxyChanged: (MapViewProxy) -> Void
4950
var proxyUpdateMode: ProxyUpdateMode
@@ -438,6 +439,10 @@ MLNMapViewDelegate {
438439
}
439440
}
440441

442+
public func mapView(_: MLNMapView, didChange mode: MLNUserTrackingMode, animated: Bool) {
443+
onUserTrackingModeChange?(mode, animated)
444+
}
445+
441446
// MARK: MapViewProxy
442447

443448
@MainActor private func updateViewProxy(mapView: MLNMapView, reason: MLNCameraChangeReason) {

Sources/MapLibreSwiftUI/MapViewModifiers.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ public extension MapView {
1414
return newMapView
1515
}
1616

17+
func onUserTrackingModeChanged(_ perform: @escaping (MLNUserTrackingMode, Bool) -> Void) -> MapView {
18+
var newMapView = self
19+
newMapView.onUserTrackingModeChanged = perform
20+
return newMapView
21+
}
22+
1723
/// Allows you to set properties of the underlying MLNMapView directly
1824
/// in cases where these have not been ported to DSL yet.
1925
/// Use this function to modify various properties of the MLNMapView instance.

0 commit comments

Comments
 (0)