Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(android): fix negative minDisplacement, fix location on v10 and v11 #3089

Merged
merged 1 commit into from
Oct 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -278,6 +272,6 @@ class LocationManager private constructor(private val context: Context) : Locati
}

init {
// buildEngineRequest()
buildEngineRequest()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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
Expand All @@ -51,4 +58,8 @@ fun LocationEngine.requestLocationUpdatesV11(callback: LocationEngineCallback, l
}

val Location.timestamp: Long
get() = this.time
get() = this.time

fun createLocationEngine(context: Context): LocationEngine {
return LocationEngineProvider.getBestLocationEngine(context)
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
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
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

Expand All @@ -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<com.mapbox.common.location.Location>) {
callback.onSuccess(LocationEngineResult(locations.last()))
}
}
class LocationEngine(var locationProvider: DeviceLocationProvider, var request: LocationProviderRequest) {
var observers: MutableList<LocationObserverAdapter> = 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) {
Expand All @@ -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
(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
}
2 changes: 1 addition & 1 deletion example/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading