diff --git a/android/src/main/java/com/rnmapbox/rnmbx/location/LocationManager.kt b/android/src/main/java/com/rnmapbox/rnmbx/location/LocationManager.kt index 583a7be0f..daa77e534 100644 --- a/android/src/main/java/com/rnmapbox/rnmbx/location/LocationManager.kt +++ b/android/src/main/java/com/rnmapbox/rnmbx/location/LocationManager.kt @@ -155,15 +155,9 @@ class LocationManager private constructor(private val context: Context) : Locati //// - /* private fun buildEngineRequest() { - engine = LocationEngineProvider.getBestLocationEngine(context.applicationContext) - locationEngineRequest = LocationEngineRequest.Builder(DEFAULT_INTERVAL_MILLIS) - .setFastestInterval(DEFAULT_FASTEST_INTERVAL_MILLIS) - .setPriority(LocationEngineRequest.PRIORITY_HIGH_ACCURACY) - .setDisplacement(mMinDisplacement) - .build() - }*/ + engine = createLocationEngine(context.applicationContext) + } fun addLocationListener(listener: OnUserLocationChange) { if (!listeners.contains(listener)) { @@ -278,6 +272,6 @@ class LocationManager private constructor(private val context: Context) : Locati } init { - // buildEngineRequest() + buildEngineRequest() } } \ No newline at end of file diff --git a/android/src/main/mapbox-v11-compat/v10/com/mapbox/rnmbx/v11compat/Location.kt b/android/src/main/mapbox-v11-compat/v10/com/mapbox/rnmbx/v11compat/Location.kt index 20cd00dc1..183973dae 100644 --- a/android/src/main/mapbox-v11-compat/v10/com/mapbox/rnmbx/v11compat/Location.kt +++ b/android/src/main/mapbox-v11-compat/v10/com/mapbox/rnmbx/v11compat/Location.kt @@ -2,6 +2,7 @@ package com.rnmapbox.rnmbx.v11compat.location; import android.Manifest.permission.ACCESS_COARSE_LOCATION import android.Manifest.permission.ACCESS_FINE_LOCATION +import android.content.Context import android.location.LocationManager import android.os.Looper import androidx.annotation.RequiresPermission @@ -10,12 +11,18 @@ import com.mapbox.maps.plugin.PuckBearingSource import com.mapbox.maps.plugin.locationcomponent.LocationComponentPlugin2 import com.mapbox.maps.plugin.locationcomponent.location2 as _location2 + import com.mapbox.android.core.location.LocationEngineResult as _LocationEngineResult import com.mapbox.android.core.location.LocationEngine as _LocationEngine import com.mapbox.android.core.location.LocationEngineCallback as _LocationEngineCallback import com.mapbox.android.core.location.LocationEngineRequest import com.mapbox.android.core.location.LocationEngineProvider +import com.mapbox.common.location.LocationService +import com.mapbox.common.location.LocationUpdatesReceiver +import com.mapbox.common.location.LocationServiceFactory + + import android.location.Location as _Location //import com.mapbox.common.location.Location as _Location @@ -37,7 +44,7 @@ fun LocationEngine.requestLocationUpdatesV11(callback: LocationEngineCallback, l val builder = LocationEngineRequest.Builder(DEFAULT_INTERVAL_MILLIS) .setFastestInterval(DEFAULT_FASTEST_INTERVAL_MILLIS) .setPriority(LocationEngineRequest.PRIORITY_HIGH_ACCURACY) - if (minDisplacement != null) { + if (minDisplacement != null && minDisplacement > 0.0) { builder.setDisplacement(minDisplacement) } val request = builder @@ -51,4 +58,8 @@ fun LocationEngine.requestLocationUpdatesV11(callback: LocationEngineCallback, l } val Location.timestamp: Long - get() = this.time \ No newline at end of file + get() = this.time + +fun createLocationEngine(context: Context): LocationEngine { + return LocationEngineProvider.getBestLocationEngine(context) +} \ No newline at end of file diff --git a/android/src/main/mapbox-v11-compat/v11/com/mapbox/rnmbx/v11compat/Location.kt b/android/src/main/mapbox-v11-compat/v11/com/mapbox/rnmbx/v11compat/Location.kt index 83131923f..06f8c2a30 100644 --- a/android/src/main/mapbox-v11-compat/v11/com/mapbox/rnmbx/v11compat/Location.kt +++ b/android/src/main/mapbox-v11-compat/v11/com/mapbox/rnmbx/v11compat/Location.kt @@ -1,6 +1,11 @@ package com.rnmapbox.rnmbx.v11compat.location; +import android.content.Context import android.os.Looper +import com.mapbox.common.location.AccuracyLevel +import com.mapbox.common.location.DeviceLocationProvider +import com.mapbox.common.location.IntervalSettings +import com.mapbox.common.location.LocationObserver import com.mapbox.common.location.Location as _Location import com.mapbox.maps.MapView import com.mapbox.maps.plugin.PuckBearing @@ -8,10 +13,17 @@ import com.mapbox.maps.plugin.locationcomponent.LocationComponentPlugin import com.mapbox.maps.plugin.locationcomponent.location import com.mapbox.common.location.LocationProvider +import com.mapbox.common.location.LocationProviderRequest +import com.mapbox.common.location.LocationService +import com.mapbox.common.location.LocationServiceFactory +import com.rnmapbox.rnmbx.utils.Logger import kotlin.math.absoluteValue typealias PuckBearingSource = PuckBearing +const val DEFAULT_FASTEST_INTERVAL_MILLIS: Long = 1000 +const val DEFAULT_INTERVAL_MILLIS: Long = 1000 + val MapView.location2 : LocationComponentPlugin get() = location @@ -32,17 +44,46 @@ interface LocationEngineCallback { fun onSuccess(locationEngineResult: LocationEngineResult) fun onFailure(e: Exception) } -class LocationEngine { - var locationProvider: LocationProvider? = null + +class LocationObserverAdapter(val callback: LocationEngineCallback): LocationObserver { + override fun onLocationUpdateReceived(locations: MutableList) { + callback.onSuccess(LocationEngineResult(locations.last())) + } +} +class LocationEngine(var locationProvider: DeviceLocationProvider, var request: LocationProviderRequest) { + var observers: MutableList = mutableListOf() } fun LocationEngine.requestLocationUpdatesV11(callback: LocationEngineCallback, looper: Looper?, minDisplacement: Float?) { - + val builder = LocationProviderRequest.Builder() + builder.interval(intervalSettings()) + builder.accuracy(AccuracyLevel.HIGH) + if (minDisplacement != null && minDisplacement > 0) { + builder.displacement(minDisplacement) + } + val request = builder.build() + if (! request.equals(this.request)) { + val newProvider = LocationServiceFactory.getOrCreate().getDeviceLocationProvider(request) + if (newProvider.isValue) { + this.locationProvider = newProvider.value!! + this.request = request + } else { + Logger.e("RNMBXLocationEngine", "Failed to get location provider: ${newProvider.error!!.message}") + } + } + val observer = LocationObserverAdapter(callback) + if (looper != null) { + locationProvider.addLocationObserver(observer, looper) + } else { + locationProvider.addLocationObserver(observer) + } + observers.add(observer) } fun LocationEngine.removeLocationUpdates(callback: LocationEngineCallback) { - + observers.filter { it.callback == callback }.forEach { locationProvider.removeLocationObserver(it) } + observers.removeAll { it.callback == callback } } fun LocationEngine.getLastLocation(callback: LocationEngineCallback) { @@ -55,4 +96,21 @@ fun LocationEngine.getLastLocation(callback: LocationEngineCallback) { val Location.accuracy: Double get() = ((this.verticalAccuracy?.absoluteValue ?: 0.0) + - (this.horizontalAccuracy?.absoluteValue ?: 0.0))/2.0 \ No newline at end of file + (this.horizontalAccuracy?.absoluteValue ?: 0.0))/2.0 + + +fun intervalSettings(): IntervalSettings { + return IntervalSettings.Builder().interval(DEFAULT_INTERVAL_MILLIS).maximumInterval( + DEFAULT_FASTEST_INTERVAL_MILLIS).build() +} +fun createLocationEngine(context: Context): LocationEngine? { + val locationService : LocationService = LocationServiceFactory.getOrCreate() + var locationProvider: DeviceLocationProvider? = null + val request = LocationProviderRequest.Builder().interval(intervalSettings()).build() + val result = locationService.getDeviceLocationProvider(request) + if (result.isValue) { + locationProvider = result.value!! + return LocationEngine(locationProvider, request) + } + return null +} \ No newline at end of file diff --git a/example/android/build.gradle b/example/android/build.gradle index 9b365dfef..4761efd3b 100644 --- a/example/android/build.gradle +++ b/example/android/build.gradle @@ -6,7 +6,7 @@ buildscript { if (project.hasProperty('RNMBX11') && project.getProperty('RNMBX11').toBoolean()) { RNMapboxMapsUseV11 = true - RNMapboxMapsVersion = '11.0.0-beta.3' + RNMapboxMapsVersion = '11.0.0-beta.4' } useCustomMapbox = false