From 98bb0da4b468aa1a30da6c2ba1cc2972615302c3 Mon Sep 17 00:00:00 2001 From: g4rb4g3 Date: Tue, 31 Oct 2023 10:47:09 +0100 Subject: [PATCH] add mapbox-10 implementation for ios --- ios/RNMBX/RNMBXMapView.swift | 109 +++++++++++++++++++++++++++++++++-- 1 file changed, 105 insertions(+), 4 deletions(-) diff --git a/ios/RNMBX/RNMBXMapView.swift b/ios/RNMBX/RNMBXMapView.swift index fadf25835..60dac4941 100644 --- a/ios/RNMBX/RNMBXMapView.swift +++ b/ios/RNMBX/RNMBXMapView.swift @@ -14,6 +14,7 @@ class FeatureEntry { } } +#if RNMBX_11 class CustomHeadingProvider: HeadingProvider { var latestHeading: Heading? private let observers: NSHashTable = .weakObjects() @@ -51,7 +52,7 @@ class CustomLocationProvider: LocationProvider { return location } - func setLocation(latitude: NSNumber, longitude: NSNumber, heading: NSNumber) { + func setLocation(latitude: NSNumber, longitude: NSNumber) { let lat = CLLocationDegrees(truncating: latitude) let lon = CLLocationDegrees(truncating: longitude) self.location = Location(clLocation: CLLocation(latitude: lat, longitude: lon)) @@ -60,6 +61,83 @@ class CustomLocationProvider: LocationProvider { } } } +#else +final public class CustomHeading: CLHeading { + private var _magneticHeading: CLLocationDirection = 0 + + public override var trueHeading: CLLocationDirection { + get { -1 } + } + + public override var magneticHeading: CLLocationDirection { + get { _magneticHeading } + set { _magneticHeading = newValue } + } +} + +class CustomLocationProvider: LocationProvider { + var locationProviderOptions: MapboxMaps.LocationOptions = .init() + + var authorizationStatus: CLAuthorizationStatus = .authorizedAlways + + var accuracyAuthorization: CLAccuracyAuthorization = .fullAccuracy + + var heading: CLHeading? + var location: CLLocation? + var updateLocation = false + var updateHeading = false + + var locationProviderDelegate: MapboxMaps.LocationProviderDelegate? + + func setDelegate(_ delegate: MapboxMaps.LocationProviderDelegate) { + locationProviderDelegate = delegate + } + + func setLocation(latitude: NSNumber, longitude: NSNumber) { + let lat = CLLocationDegrees(truncating: latitude) + let lon = CLLocationDegrees(truncating: longitude) + self.location = CLLocation(latitude: lat, longitude: lon) + if (updateLocation) { + locationProviderDelegate?.locationProvider(self, didUpdateLocations: [self.location!]) + } + } + + func setHeading(heading: NSNumber) { + let latestHeading = CustomHeading() + latestHeading.magneticHeading = CLLocationDirection(truncating: heading) + self.heading = latestHeading + if (self.updateHeading) { + locationProviderDelegate?.locationProvider(self, didUpdateHeading: self.heading!) + } + } + + func requestAlwaysAuthorization() { } + + func requestWhenInUseAuthorization() { } + + func requestTemporaryFullAccuracyAuthorization(withPurposeKey purposeKey: String) { } + + func startUpdatingLocation() { + self.updateLocation = true + } + + func stopUpdatingLocation() { + self.updateLocation = false + } + + var headingOrientation: CLDeviceOrientation = .unknown + + func startUpdatingHeading() { + self.updateHeading = true + } + + func stopUpdatingHeading() { + self.updateHeading = false + } + + func dismissHeadingCalibrationDisplay() { } +} +#endif #if RNMBX_11 extension QueriedRenderedFeature { @@ -196,10 +274,14 @@ open class RNMBXMapView : MapView { var mapView : MapView { get { return self } } - + var customLocationProvider: CustomLocationProvider? = nil + #if RNMBX_11 var customHeadingProvider: CustomHeadingProvider? = nil - + #else + var defaultLocationProvider: LocationProvider? + #endif + @objc public func addToMap(_ subview: UIView) { if let mapComponent = subview as? RNMBXMapComponent { let style = mapView.mapboxMap.style @@ -590,20 +672,39 @@ open class RNMBXMapView : MapView { longitude: NSNumber, heading: NSNumber ) -> Void { + #if RNMBX_11 if (customLocationProvider == nil && customHeadingProvider == nil) { customLocationProvider = CustomLocationProvider() customHeadingProvider = CustomHeadingProvider() mapView.location.override(locationProvider: customLocationProvider!, headingProvider: customHeadingProvider) } - customLocationProvider?.setLocation(latitude: latitude, longitude: longitude, heading: heading) + customLocationProvider?.setLocation(latitude: latitude, longitude: longitude) customHeadingProvider?.setHeading(heading: heading) + #else + if (customLocationProvider == nil) { + if (defaultLocationProvider == nil) { + defaultLocationProvider = mapView.location.locationProvider + } + customLocationProvider = CustomLocationProvider() + mapView.location.overrideLocationProvider(with: customLocationProvider!) + } + customLocationProvider?.setLocation(latitude: latitude, longitude: longitude) + customLocationProvider?.setHeading(heading: heading) + #endif } func removeCustomLocationProvider() { + #if RNMBX_11 mapView.location.override(provider: AppleLocationProvider()) customLocationProvider = nil customHeadingProvider = nil + #else + if let provider = defaultLocationProvider { + mapView.location.overrideLocationProvider(with: provider) + } + customLocationProvider = nil + #endif } }