@rnmapbox/maps 10.1.0-beta.8 → 10.1.0-beta.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -155,15 +155,9 @@ class LocationManager private constructor(private val context: Context) : Locati
155
155
 
156
156
  ////
157
157
 
158
- /*
159
158
  private fun buildEngineRequest() {
160
- engine = LocationEngineProvider.getBestLocationEngine(context.applicationContext)
161
- locationEngineRequest = LocationEngineRequest.Builder(DEFAULT_INTERVAL_MILLIS)
162
- .setFastestInterval(DEFAULT_FASTEST_INTERVAL_MILLIS)
163
- .setPriority(LocationEngineRequest.PRIORITY_HIGH_ACCURACY)
164
- .setDisplacement(mMinDisplacement)
165
- .build()
166
- }*/
159
+ engine = createLocationEngine(context.applicationContext)
160
+ }
167
161
 
168
162
  fun addLocationListener(listener: OnUserLocationChange) {
169
163
  if (!listeners.contains(listener)) {
@@ -278,6 +272,6 @@ class LocationManager private constructor(private val context: Context) : Locati
278
272
  }
279
273
 
280
274
  init {
281
- // buildEngineRequest()
275
+ buildEngineRequest()
282
276
  }
283
277
  }
@@ -2,6 +2,7 @@ package com.rnmapbox.rnmbx.v11compat.location;
2
2
 
3
3
  import android.Manifest.permission.ACCESS_COARSE_LOCATION
4
4
  import android.Manifest.permission.ACCESS_FINE_LOCATION
5
+ import android.content.Context
5
6
  import android.location.LocationManager
6
7
  import android.os.Looper
7
8
  import androidx.annotation.RequiresPermission
@@ -10,12 +11,18 @@ import com.mapbox.maps.plugin.PuckBearingSource
10
11
  import com.mapbox.maps.plugin.locationcomponent.LocationComponentPlugin2
11
12
  import com.mapbox.maps.plugin.locationcomponent.location2 as _location2
12
13
 
14
+
13
15
  import com.mapbox.android.core.location.LocationEngineResult as _LocationEngineResult
14
16
 
15
17
  import com.mapbox.android.core.location.LocationEngine as _LocationEngine
16
18
  import com.mapbox.android.core.location.LocationEngineCallback as _LocationEngineCallback
17
19
  import com.mapbox.android.core.location.LocationEngineRequest
18
20
  import com.mapbox.android.core.location.LocationEngineProvider
21
+ import com.mapbox.common.location.LocationService
22
+ import com.mapbox.common.location.LocationUpdatesReceiver
23
+ import com.mapbox.common.location.LocationServiceFactory
24
+
25
+
19
26
  import android.location.Location as _Location
20
27
  //import com.mapbox.common.location.Location as _Location
21
28
 
@@ -37,7 +44,7 @@ fun LocationEngine.requestLocationUpdatesV11(callback: LocationEngineCallback, l
37
44
  val builder = LocationEngineRequest.Builder(DEFAULT_INTERVAL_MILLIS)
38
45
  .setFastestInterval(DEFAULT_FASTEST_INTERVAL_MILLIS)
39
46
  .setPriority(LocationEngineRequest.PRIORITY_HIGH_ACCURACY)
40
- if (minDisplacement != null) {
47
+ if (minDisplacement != null && minDisplacement > 0.0) {
41
48
  builder.setDisplacement(minDisplacement)
42
49
  }
43
50
  val request = builder
@@ -51,4 +58,8 @@ fun LocationEngine.requestLocationUpdatesV11(callback: LocationEngineCallback, l
51
58
  }
52
59
 
53
60
  val Location.timestamp: Long
54
- get() = this.time
61
+ get() = this.time
62
+
63
+ fun createLocationEngine(context: Context): LocationEngine {
64
+ return LocationEngineProvider.getBestLocationEngine(context)
65
+ }
@@ -1,6 +1,11 @@
1
1
  package com.rnmapbox.rnmbx.v11compat.location;
2
2
 
3
+ import android.content.Context
3
4
  import android.os.Looper
5
+ import com.mapbox.common.location.AccuracyLevel
6
+ import com.mapbox.common.location.DeviceLocationProvider
7
+ import com.mapbox.common.location.IntervalSettings
8
+ import com.mapbox.common.location.LocationObserver
4
9
  import com.mapbox.common.location.Location as _Location
5
10
  import com.mapbox.maps.MapView
6
11
  import com.mapbox.maps.plugin.PuckBearing
@@ -8,10 +13,17 @@ import com.mapbox.maps.plugin.locationcomponent.LocationComponentPlugin
8
13
  import com.mapbox.maps.plugin.locationcomponent.location
9
14
 
10
15
  import com.mapbox.common.location.LocationProvider
16
+ import com.mapbox.common.location.LocationProviderRequest
17
+ import com.mapbox.common.location.LocationService
18
+ import com.mapbox.common.location.LocationServiceFactory
19
+ import com.rnmapbox.rnmbx.utils.Logger
11
20
  import kotlin.math.absoluteValue
12
21
 
13
22
  typealias PuckBearingSource = PuckBearing
14
23
 
24
+ const val DEFAULT_FASTEST_INTERVAL_MILLIS: Long = 1000
25
+ const val DEFAULT_INTERVAL_MILLIS: Long = 1000
26
+
15
27
  val MapView.location2 : LocationComponentPlugin
16
28
  get() = location
17
29
 
@@ -32,17 +44,46 @@ interface LocationEngineCallback {
32
44
  fun onSuccess(locationEngineResult: LocationEngineResult)
33
45
  fun onFailure(e: Exception)
34
46
  }
35
- class LocationEngine {
36
- var locationProvider: LocationProvider? = null
47
+
48
+ class LocationObserverAdapter(val callback: LocationEngineCallback): LocationObserver {
49
+ override fun onLocationUpdateReceived(locations: MutableList<com.mapbox.common.location.Location>) {
50
+ callback.onSuccess(LocationEngineResult(locations.last()))
51
+ }
52
+ }
53
+ class LocationEngine(var locationProvider: DeviceLocationProvider, var request: LocationProviderRequest) {
54
+ var observers: MutableList<LocationObserverAdapter> = mutableListOf()
37
55
  }
38
56
 
39
57
 
40
58
  fun LocationEngine.requestLocationUpdatesV11(callback: LocationEngineCallback, looper: Looper?, minDisplacement: Float?) {
41
-
59
+ val builder = LocationProviderRequest.Builder()
60
+ builder.interval(intervalSettings())
61
+ builder.accuracy(AccuracyLevel.HIGH)
62
+ if (minDisplacement != null && minDisplacement > 0) {
63
+ builder.displacement(minDisplacement)
64
+ }
65
+ val request = builder.build()
66
+ if (! request.equals(this.request)) {
67
+ val newProvider = LocationServiceFactory.getOrCreate().getDeviceLocationProvider(request)
68
+ if (newProvider.isValue) {
69
+ this.locationProvider = newProvider.value!!
70
+ this.request = request
71
+ } else {
72
+ Logger.e("RNMBXLocationEngine", "Failed to get location provider: ${newProvider.error!!.message}")
73
+ }
74
+ }
75
+ val observer = LocationObserverAdapter(callback)
76
+ if (looper != null) {
77
+ locationProvider.addLocationObserver(observer, looper)
78
+ } else {
79
+ locationProvider.addLocationObserver(observer)
80
+ }
81
+ observers.add(observer)
42
82
  }
43
83
 
44
84
  fun LocationEngine.removeLocationUpdates(callback: LocationEngineCallback) {
45
-
85
+ observers.filter { it.callback == callback }.forEach { locationProvider.removeLocationObserver(it) }
86
+ observers.removeAll { it.callback == callback }
46
87
  }
47
88
 
48
89
  fun LocationEngine.getLastLocation(callback: LocationEngineCallback) {
@@ -55,4 +96,21 @@ fun LocationEngine.getLastLocation(callback: LocationEngineCallback) {
55
96
 
56
97
  val Location.accuracy: Double
57
98
  get() = ((this.verticalAccuracy?.absoluteValue ?: 0.0) +
58
- (this.horizontalAccuracy?.absoluteValue ?: 0.0))/2.0
99
+ (this.horizontalAccuracy?.absoluteValue ?: 0.0))/2.0
100
+
101
+
102
+ fun intervalSettings(): IntervalSettings {
103
+ return IntervalSettings.Builder().interval(DEFAULT_INTERVAL_MILLIS).maximumInterval(
104
+ DEFAULT_FASTEST_INTERVAL_MILLIS).build()
105
+ }
106
+ fun createLocationEngine(context: Context): LocationEngine? {
107
+ val locationService : LocationService = LocationServiceFactory.getOrCreate()
108
+ var locationProvider: DeviceLocationProvider? = null
109
+ val request = LocationProviderRequest.Builder().interval(intervalSettings()).build()
110
+ val result = locationService.getDeviceLocationProvider(request)
111
+ if (result.isValue) {
112
+ locationProvider = result.value!!
113
+ return LocationEngine(locationProvider, request)
114
+ }
115
+ return null
116
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@rnmapbox/maps",
3
3
  "description": "A Mapbox react native module for creating custom maps",
4
- "version": "10.1.0-beta.8",
4
+ "version": "10.1.0-beta.9",
5
5
  "publishConfig": {
6
6
  "access": "public"
7
7
  },