feat: add SpeedCalculator to DistanceFilterProcessor for manual speed calculation

This commit is contained in:
fiatcode 2026-05-02 16:18:14 +07:00
parent 50b60e31b7
commit eca39e542c
No known key found for this signature in database

View file

@ -18,6 +18,14 @@ class DistanceFilterProcessor {
private var lastLongitude: Double? = null
private var lastTimestamp: Long = 0
private var lastHeading: Float? = null
private var speedCalculator: SpeedCalculator? = null
fun getCalculatedSpeed(location: Location): Float? {
if (speedCalculator == null) {
speedCalculator = SpeedCalculator()
}
return speedCalculator?.calculateSpeed(location.latitude, location.longitude, location.time)
}
fun shouldAccept(location: Location, config: FilterConfig): Boolean {
val now = System.currentTimeMillis()
@ -72,6 +80,46 @@ class DistanceFilterProcessor {
lastLongitude = null
lastTimestamp = 0
lastHeading = null
speedCalculator?.reset()
}
class SpeedCalculator {
private var lastLat: Double = 0.0
private var lastLon: Double = 0.0
private var lastTime: Long = 0
fun calculateSpeed(lat: Double, lon: Double, time: Long): Float? {
if (lastLat == 0.0 && lastLon == 0.0 && lastTime == 0L) {
lastLat = lat
lastLon = lon
lastTime = time
return null
}
val distance = haversineDistance(lastLat, lastLon, lat, lon)
val timeDelta = (time - lastTime) / 1000.0
if (timeDelta <= 0) {
lastLat = lat
lastLon = lon
lastTime = time
return null
}
val speed = (distance / timeDelta).toFloat()
lastLat = lat
lastLon = lon
lastTime = time
return speed
}
fun reset() {
lastLat = 0.0
lastLon = 0.0
lastTime = 0
}
}
companion object {