mapbox / mapbox-maps-android

Interactive, thoroughly customizable maps in native Android powered by vector tiles and OpenGL.
https://www.mapbox.com/mobile-maps-sdk
Other
475 stars 133 forks source link

Ability to add an update interval to onIndicatorPositionChangedListener #2130

Closed okcoker closed 1 year ago

okcoker commented 1 year ago

New Feature

It would be nice if the LocationComponentSettings had an option to filter position updates either by time and/or distance.

Why

locationComponentPlugin.addOnIndicatorPositionChangedListener() updates pretty frequently. To achieve this I have to manually add my own logic similar to this:

private const val UPDATE_INTERVAL_MS = 1000

private const val UPDATE_DISTANCE_METERS = 10.0

private var lastUpdateTime: Long = 0

private var lastLocationPoint: Point? = null

private val indicatorPositionChangedListener = OnIndicatorPositionChangedListener { point ->
    val currentTime = SystemClock.elapsedRealtime()
    val elapsedTime = currentTime - lastUpdateTime

    if (elapsedTime >= UPDATE_INTERVAL_MS || distanceBetweenPoints(point, lastLocationPoint) >= UPDATE_DISTANCE_METERS) {

        lastUpdateTime = currentTime
    }

     lastLocationPoint = point
}
kiryldz commented 1 year ago

Feel free to introduce your own LocationProvider and implement the logic you need for the frequency updates. Rough example how to do this could be found here: https://github.com/mapbox/mapbox-maps-android/blob/main/app/src/main/java/com/mapbox/maps/testapp/examples/LocationComponentAnimationActivity.kt. OnIndicatorPositionChangedListener itself triggers on each location update without any preconditions.