Debounce implemented
This commit is contained in:
parent
7b397e78bd
commit
801d00c6d0
@ -24,6 +24,7 @@ import com.dumon.plugin.geolocation.utils.PermissionUtils
|
|||||||
import com.getcapacitor.annotation.PermissionCallback
|
import com.getcapacitor.annotation.PermissionCallback
|
||||||
import org.json.JSONArray
|
import org.json.JSONArray
|
||||||
import org.json.JSONObject
|
import org.json.JSONObject
|
||||||
|
import kotlin.math.*
|
||||||
|
|
||||||
@CapacitorPlugin(
|
@CapacitorPlugin(
|
||||||
name = "DumonGeolocation",
|
name = "DumonGeolocation",
|
||||||
@ -56,7 +57,18 @@ class DumonGeolocation : Plugin() {
|
|||||||
|
|
||||||
private var isMockedLocation = false
|
private var isMockedLocation = false
|
||||||
private var lastEmitTimestamp: Long = 0L
|
private var lastEmitTimestamp: Long = 0L
|
||||||
private val emitIntervalMs: Long = 500L
|
|
||||||
|
private var prevLatitude = 0.0
|
||||||
|
private var prevLongitude = 0.0
|
||||||
|
private var prevSpeed = 0f
|
||||||
|
private var prevDirection = 0f
|
||||||
|
// private val significantChangeThreshold = 0.00007 // ~7 meters
|
||||||
|
private val significantChangeThreshold = 7 // ~7 meters
|
||||||
|
private val speedChangeThreshold = 0.5f // m/s
|
||||||
|
private val directionChangeThreshold = 0.17f // ~10 deg
|
||||||
|
|
||||||
|
// private val emitIntervalMs: Long = 500L
|
||||||
|
private val emitIntervalMs: Long = 1000L // hard debounce
|
||||||
|
|
||||||
override fun load() {
|
override fun load() {
|
||||||
gpsManager = GpsStatusManager(
|
gpsManager = GpsStatusManager(
|
||||||
@ -231,11 +243,47 @@ class DumonGeolocation : Plugin() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun emitPositionUpdate() {
|
private fun emitPositionUpdate() {
|
||||||
val now = System.currentTimeMillis()
|
/*val now = System.currentTimeMillis()
|
||||||
if (now - lastEmitTimestamp < emitIntervalMs) return
|
if (now - lastEmitTimestamp < emitIntervalMs) return
|
||||||
lastEmitTimestamp = now
|
lastEmitTimestamp = now
|
||||||
|
notifyListeners("onPositionUpdate", buildPositionData())*/
|
||||||
|
|
||||||
|
val now = System.currentTimeMillis()
|
||||||
|
if (now - lastEmitTimestamp < emitIntervalMs) return
|
||||||
|
|
||||||
|
val distance = calculateDistance(latestLatitude, latestLongitude, prevLatitude, prevLongitude)
|
||||||
|
val isSignificantChange = distance >= significantChangeThreshold
|
||||||
|
val speedChanged = abs((latestImu?.speed ?: 0f) - prevSpeed) > speedChangeThreshold
|
||||||
|
val directionChanged = abs((latestImu?.directionRad ?: 0f) - prevDirection) > directionChangeThreshold
|
||||||
|
|
||||||
|
if (isSignificantChange || speedChanged || directionChanged) {
|
||||||
|
prevLatitude = latestLatitude
|
||||||
|
prevLongitude = latestLongitude
|
||||||
|
prevSpeed = latestImu?.speed ?: 0f
|
||||||
|
prevDirection = latestImu?.directionRad ?: 0f
|
||||||
|
lastEmitTimestamp = now
|
||||||
|
|
||||||
notifyListeners("onPositionUpdate", buildPositionData())
|
notifyListeners("onPositionUpdate", buildPositionData())
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun degToRad(deg: Double): Double {
|
||||||
|
return deg * PI / 180.0
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun calculateDistance(lat1: Double, lon1: Double, lat2: Double, lon2: Double): Double {
|
||||||
|
val R = 6371000.0 // Radius bumi dalam meter
|
||||||
|
|
||||||
|
val latDistance = degToRad(lat2 - lat1)
|
||||||
|
val lonDistance = degToRad(lon2 - lon1)
|
||||||
|
|
||||||
|
val a = sin(latDistance / 2).pow(2) + cos(degToRad(lat1)) * cos(degToRad(lat2)) *
|
||||||
|
sin(lonDistance / 2).pow(2)
|
||||||
|
|
||||||
|
val c = 2 * atan2(sqrt(a), sqrt(1 - a))
|
||||||
|
|
||||||
|
return R * c
|
||||||
|
}
|
||||||
|
|
||||||
private fun buildPositionData(): JSObject {
|
private fun buildPositionData(): JSObject {
|
||||||
val obj = JSObject()
|
val obj = JSObject()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user