refactor(shared): centralize distanceMeters util and remove local copies

This commit is contained in:
2025-09-07 23:27:19 +02:00
parent f7c79ed420
commit 5d348ac7cc
5 changed files with 35 additions and 53 deletions

View File

@@ -0,0 +1,29 @@
package app.klottr.util
import kotlin.math.PI
import kotlin.math.atan2
import kotlin.math.cos
import kotlin.math.sin
import kotlin.math.sqrt
/**
* Haversine: beräkna avstånd i meter mellan två lat/lon-punkter.
* Ren KMP-funktion (ingen plattforms-API).
*/
fun distanceMeters(aLat: Double, aLon: Double, bLat: Double, bLon: Double): Double {
val r = 6371000.0 // jordradie i meter
// Konvertera grader → radianer (utan java.lang.Math)
val degToRad = PI / 180.0
val dLat = (bLat - aLat) * degToRad
val dLon = (bLon - aLon) * degToRad
val aLatRad = aLat * degToRad
val bLatRad = bLat * degToRad
val s1 = sin(dLat / 2.0)
val s2 = sin(dLon / 2.0)
val aa = s1 * s1 + cos(aLatRad) * cos(bLatRad) * s2 * s2
val c = 2.0 * atan2(sqrt(aa), sqrt(1.0 - aa))
return r * c
}