Skip to content

Commit

Permalink
fix(ios): fix custome location provider add/remove (#3416)
Browse files Browse the repository at this point in the history
  • Loading branch information
mfazekas authored Mar 10, 2024
1 parent f4a501c commit 3437362
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
13 changes: 10 additions & 3 deletions ios/RNMBX/RNMBXCustomLocationProvider.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import MapboxMaps

let TAG = "RNMBXCustomLocationProvider"

@objc
public class RNMBXCustomLocationProvider: UIView, RNMBXMapComponent {
var map: RNMBXMapView? = nil
Expand Down Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions ios/RNMBX/RNMBXLocationModule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,7 @@ class RNMBXLocationModule: RCTEventEmitter, LocationProviderRNMBXDelegate {
var hasListener = false

var locationProvider : LocationProvider
var defaultLocationProvider : LocationProvider? = nil

var locationEventThrottle : (
waitBetweenEvents: Double?,
Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 4 additions & 0 deletions ios/RNMBX/RNMBXLogging.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 3437362

Please sign in to comment.