From 34373620f143851f95372ff6f3d3c12672bfc247 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mikl=C3=B3s=20Fazekas?= Date: Sun, 10 Mar 2024 08:59:13 +0100 Subject: [PATCH] fix(ios): fix custome location provider add/remove (#3416) --- ios/RNMBX/RNMBXCustomLocationProvider.swift | 13 ++++++++++--- ios/RNMBX/RNMBXLocationModule.swift | 8 ++++++++ ios/RNMBX/RNMBXLogging.swift | 4 ++++ 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/ios/RNMBX/RNMBXCustomLocationProvider.swift b/ios/RNMBX/RNMBXCustomLocationProvider.swift index 5abf2e3dd..8ff3c5480 100644 --- a/ios/RNMBX/RNMBXCustomLocationProvider.swift +++ b/ios/RNMBX/RNMBXCustomLocationProvider.swift @@ -1,5 +1,7 @@ import MapboxMaps +let TAG = "RNMBXCustomLocationProvider" + @objc public class RNMBXCustomLocationProvider: UIView, RNMBXMapComponent { var map: RNMBXMapView? = nil @@ -118,16 +120,21 @@ extension RNMBXCustomLocationProvider { let customLocationProvider = CustomLocationProvider() self.customLocationProvider = customLocationProvider if let locationModule = RNMBXLocationModule.shared { - locationModule.override(for: mapView.location) locationModule.locationProvider = customLocationProvider - // mapView.location.overrideLocationProvider(with: customLocationProvider!) + locationModule.override(for: mapView.location) + } else { + Logger.error(TAG, "RNMBXLocationModule.shared is nil") + mapView.location.overrideLocationProvider(with: customLocationProvider) } } } func removeCustomLocationProvider(mapView: MapView) { - if let provider = defaultLocationProvider { + if let locationModule = RNMBXLocationModule.shared { + locationModule.resetLocationProvider() + locationModule.override(for: mapView.location) + } else if let provider = defaultLocationProvider { mapView.location.overrideLocationProvider(with: provider) } customLocationProvider = nil diff --git a/ios/RNMBX/RNMBXLocationModule.swift b/ios/RNMBX/RNMBXLocationModule.swift index c31f2a310..7d7be80cf 100644 --- a/ios/RNMBX/RNMBXLocationModule.swift +++ b/ios/RNMBX/RNMBXLocationModule.swift @@ -460,6 +460,7 @@ class RNMBXLocationModule: RCTEventEmitter, LocationProviderRNMBXDelegate { var hasListener = false var locationProvider : LocationProvider + var defaultLocationProvider : LocationProvider? = nil var locationEventThrottle : ( waitBetweenEvents: Double?, @@ -471,6 +472,7 @@ class RNMBXLocationModule: RCTEventEmitter, LocationProviderRNMBXDelegate { override init() { locationProvider = LocationProviderRNMBX() + defaultLocationProvider = locationProvider super.init() if let locationProvider = locationProvider as? LocationProviderRNMBX { locationProvider.delegate = self @@ -568,6 +570,12 @@ class RNMBXLocationModule: RCTEventEmitter, LocationProviderRNMBXDelegate { } } + func resetLocationProvider() { + if let defaultLocationProvider = defaultLocationProvider { + self.locationProvider = defaultLocationProvider + } + } + // MARK: - location event throttle @objc func setLocationEventThrottle(_ throttleValue:NSNumber) { diff --git a/ios/RNMBX/RNMBXLogging.swift b/ios/RNMBX/RNMBXLogging.swift index 50fbd02c4..a5273221a 100644 --- a/ios/RNMBX/RNMBXLogging.swift +++ b/ios/RNMBX/RNMBXLogging.swift @@ -78,6 +78,10 @@ public class Logger { } } + public static func error(_ tag: String, _ message: String) { + log(level: .error, tag: tag, message: message) + } + public static func error(_ message: String) { log(level: .error, message: message) }