Skip to content

Commit 738428c

Browse files
authored
Update on change of location manager (#142)
1 parent a6b9f80 commit 738428c

2 files changed

Lines changed: 22 additions & 0 deletions

File tree

Sources/MapLibreSwiftUI/MapView.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ public struct MapView<T: MapViewHostViewController>: UIViewControllerRepresentab
8484

8585
controller.mapView.locationManager = locationManager
8686
controller.mapView.locationManager = controller.mapView.locationManager
87+
context.coordinator.isUsingDefaultLocationManager = locationManager == nil
8788

8889
switch styleSource {
8990
case let .url(styleURL):
@@ -119,6 +120,26 @@ public struct MapView<T: MapViewHostViewController>: UIViewControllerRepresentab
119120

120121
applyModifiers(uiViewController, runUnsafe: true)
121122

123+
// `nil` here means "use MapLibre's built-in default manager", but
124+
// `mapView.locationManager` will hold a concrete manager instance, not nil.
125+
// We therefore track whether we are currently in "default manager mode"
126+
// instead of relying on direct object identity against nil.
127+
if let desiredLocationManager = locationManager {
128+
let currentLocationManager = uiViewController.mapView.locationManager as AnyObject?
129+
let desiredLocationManagerObject = desiredLocationManager as AnyObject
130+
if context.coordinator
131+
.isUsingDefaultLocationManager || currentLocationManager !== desiredLocationManagerObject
132+
{
133+
uiViewController.mapView.locationManager = desiredLocationManager
134+
uiViewController.mapView.locationManager = uiViewController.mapView.locationManager
135+
context.coordinator.isUsingDefaultLocationManager = false
136+
}
137+
} else if !context.coordinator.isUsingDefaultLocationManager {
138+
uiViewController.mapView.locationManager = nil
139+
uiViewController.mapView.locationManager = uiViewController.mapView.locationManager
140+
context.coordinator.isUsingDefaultLocationManager = true
141+
}
142+
122143
// FIXME: This should be a more selective update
123144
context.coordinator.updateStyleSource(styleSource, mapView: uiViewController.mapView)
124145
context.coordinator.updateLayers(mapView: uiViewController.mapView)

Sources/MapLibreSwiftUI/MapViewCoordinator.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ MLNMapViewDelegate {
5454
var onViewProxyChanged: (MapViewProxy) -> Void
5555
var proxyUpdateMode: ProxyUpdateMode
5656
var managedGestureRecognizers: [UIGestureRecognizer] = []
57+
var isUsingDefaultLocationManager = true
5758

5859
/// Debounces rapid-fire tile error callbacks so the consumer receives one
5960
/// notification per burst rather than one per individual failing tile.

0 commit comments

Comments
 (0)