Skip to content

Commit 3437362

Browse files
authored
fix(ios): fix custome location provider add/remove (#3416)
1 parent f4a501c commit 3437362

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

ios/RNMBX/RNMBXCustomLocationProvider.swift

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import MapboxMaps
22

3+
let TAG = "RNMBXCustomLocationProvider"
4+
35
@objc
46
public class RNMBXCustomLocationProvider: UIView, RNMBXMapComponent {
57
var map: RNMBXMapView? = nil
@@ -118,16 +120,21 @@ extension RNMBXCustomLocationProvider {
118120
let customLocationProvider = CustomLocationProvider()
119121
self.customLocationProvider = customLocationProvider
120122
if let locationModule = RNMBXLocationModule.shared {
121-
locationModule.override(for: mapView.location)
122123
locationModule.locationProvider = customLocationProvider
123-
// mapView.location.overrideLocationProvider(with: customLocationProvider!)
124+
locationModule.override(for: mapView.location)
125+
} else {
126+
Logger.error(TAG, "RNMBXLocationModule.shared is nil")
127+
mapView.location.overrideLocationProvider(with: customLocationProvider)
124128
}
125129

126130
}
127131
}
128132

129133
func removeCustomLocationProvider(mapView: MapView) {
130-
if let provider = defaultLocationProvider {
134+
if let locationModule = RNMBXLocationModule.shared {
135+
locationModule.resetLocationProvider()
136+
locationModule.override(for: mapView.location)
137+
} else if let provider = defaultLocationProvider {
131138
mapView.location.overrideLocationProvider(with: provider)
132139
}
133140
customLocationProvider = nil

ios/RNMBX/RNMBXLocationModule.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,7 @@ class RNMBXLocationModule: RCTEventEmitter, LocationProviderRNMBXDelegate {
460460
var hasListener = false
461461

462462
var locationProvider : LocationProvider
463+
var defaultLocationProvider : LocationProvider? = nil
463464

464465
var locationEventThrottle : (
465466
waitBetweenEvents: Double?,
@@ -471,6 +472,7 @@ class RNMBXLocationModule: RCTEventEmitter, LocationProviderRNMBXDelegate {
471472

472473
override init() {
473474
locationProvider = LocationProviderRNMBX()
475+
defaultLocationProvider = locationProvider
474476
super.init()
475477
if let locationProvider = locationProvider as? LocationProviderRNMBX {
476478
locationProvider.delegate = self
@@ -568,6 +570,12 @@ class RNMBXLocationModule: RCTEventEmitter, LocationProviderRNMBXDelegate {
568570
}
569571
}
570572

573+
func resetLocationProvider() {
574+
if let defaultLocationProvider = defaultLocationProvider {
575+
self.locationProvider = defaultLocationProvider
576+
}
577+
}
578+
571579
// MARK: - location event throttle
572580
@objc
573581
func setLocationEventThrottle(_ throttleValue:NSNumber) {

ios/RNMBX/RNMBXLogging.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ public class Logger {
7878
}
7979
}
8080

81+
public static func error(_ tag: String, _ message: String) {
82+
log(level: .error, tag: tag, message: message)
83+
}
84+
8185
public static func error(_ message: String) {
8286
log(level: .error, message: message)
8387
}

0 commit comments

Comments
 (0)