Skip to content

Commit

Permalink
add mapbox-10 implementation for ios
Browse files Browse the repository at this point in the history
  • Loading branch information
g4rb4g3 committed Oct 31, 2023
1 parent 33a54bf commit 98bb0da
Showing 1 changed file with 105 additions and 4 deletions.
109 changes: 105 additions & 4 deletions ios/RNMBX/RNMBXMapView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class FeatureEntry {
}
}

#if RNMBX_11
class CustomHeadingProvider: HeadingProvider {
var latestHeading: Heading?
private let observers: NSHashTable<AnyObject> = .weakObjects()
Expand Down Expand Up @@ -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))
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
}

Expand Down

0 comments on commit 98bb0da

Please sign in to comment.