refactor(shared): centralize distanceMeters util and remove local copies
This commit is contained in:
29
shared/src/commonMain/kotlin/app/klottr/util/Geo.kt
Normal file
29
shared/src/commonMain/kotlin/app/klottr/util/Geo.kt
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user