refactor(shared): centralize distanceMeters util and remove local copies
This commit is contained in:
@@ -17,6 +17,7 @@ import app.klottr.net.NoteDTO
|
||||
import app.klottr.util.FoundStore
|
||||
import app.klottr.util.LastLocationStore
|
||||
import app.klottr.util.SettingsStore
|
||||
import app.klottr.util.distanceMeters
|
||||
import com.google.android.gms.location.LocationCallback
|
||||
import com.google.android.gms.location.LocationRequest
|
||||
import com.google.android.gms.location.LocationResult
|
||||
@@ -325,16 +326,3 @@ class LocationService : Service() {
|
||||
.build()
|
||||
}
|
||||
}
|
||||
|
||||
// ---- Hjälp ----
|
||||
|
||||
private fun distanceMeters(aLat: Double, aLon: Double, bLat: Double, bLon: Double): Double {
|
||||
val R = 6371000.0
|
||||
val dLat = Math.toRadians(bLat - aLat)
|
||||
val dLon = Math.toRadians(bLon - aLon)
|
||||
val s1 = Math.sin(dLat / 2)
|
||||
val s2 = Math.sin(dLon / 2)
|
||||
val aa = s1 * s1 + Math.cos(Math.toRadians(aLat)) * Math.cos(Math.toRadians(bLat)) * s2 * s2
|
||||
val c = 2 * Math.atan2(Math.sqrt(aa), Math.sqrt(1 - aa))
|
||||
return R * c
|
||||
}
|
||||
|
||||
@@ -116,6 +116,7 @@ import app.klottr.util.SettingsStore
|
||||
import app.klottr.util.FoundStore.LAYER_DM
|
||||
import app.klottr.util.FoundStore.LAYER_KLOTTER
|
||||
import app.klottr.util.FoundStore.LAYER_WIKI
|
||||
import app.klottr.util.distanceMeters
|
||||
import app.klottr.util.formatDistance
|
||||
import app.klottr.util.formatRelativeOrTime
|
||||
import app.klottr.util.formatCoords
|
||||
@@ -2890,17 +2891,6 @@ fun hasBgLocationPerm(ctx: Context): Boolean {
|
||||
) == PackageManager.PERMISSION_GRANTED
|
||||
}
|
||||
|
||||
private fun distanceMeters(aLat: Double, aLon: Double, bLat: Double, bLon: Double): Double {
|
||||
val R = 6371000.0
|
||||
val dLat = Math.toRadians(bLat - aLat)
|
||||
val dLon = Math.toRadians(bLon - aLon)
|
||||
val s1 = Math.sin(dLat / 2)
|
||||
val s2 = Math.sin(dLon / 2)
|
||||
val aa = s1 * s1 + Math.cos(Math.toRadians(aLat)) * Math.cos(Math.toRadians(bLat)) * s2 * s2
|
||||
val c = 2 * Math.atan2(Math.sqrt(aa), Math.sqrt(1 - aa))
|
||||
return R * c
|
||||
}
|
||||
|
||||
private fun fetchBitmapFromUrl(url: String): Bitmap? {
|
||||
return try {
|
||||
val conn = java.net.URL(url).openConnection() as java.net.HttpURLConnection
|
||||
|
||||
@@ -28,6 +28,7 @@ import app.klottr.net.Wikipedia
|
||||
import app.klottr.net.WikipediaPage
|
||||
import app.klottr.util.Config
|
||||
import app.klottr.util.SettingsStore
|
||||
import app.klottr.util.distanceMeters
|
||||
import com.google.android.gms.location.LocationServices as GmsLocationServices
|
||||
import com.google.android.gms.location.Priority
|
||||
import com.google.android.gms.maps.model.CameraPosition
|
||||
@@ -372,15 +373,3 @@ private fun openWalkingDirections(ctx: Context, lat: Double?, lon: Double?, titl
|
||||
openInMaps(ctx, lat, lon, title)
|
||||
}
|
||||
}
|
||||
|
||||
// Samma distans som i MainActivity
|
||||
private fun distanceMeters(aLat: Double, aLon: Double, bLat: Double, bLon: Double): Double {
|
||||
val R = 6371000.0
|
||||
val dLat = Math.toRadians(bLat - aLat)
|
||||
val dLon = Math.toRadians(bLon - aLon)
|
||||
val s1 = Math.sin(dLat / 2)
|
||||
val s2 = Math.sin(dLon / 2)
|
||||
val aa = s1 * s1 + Math.cos(Math.toRadians(aLat)) * Math.cos(Math.toRadians(bLat)) * s2 * s2
|
||||
val c = 2 * Math.atan2(Math.sqrt(aa), Math.sqrt(1 - aa))
|
||||
return R * c
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
package app.klottr.util
|
||||
|
||||
import app.klottr.util.distanceMeters
|
||||
import android.content.Context
|
||||
import org.json.JSONObject
|
||||
|
||||
@@ -167,22 +167,8 @@ object FoundStore {
|
||||
touched = true
|
||||
}
|
||||
}
|
||||
|
||||
if (touched) e.apply()
|
||||
}
|
||||
|
||||
/** Samma Haversine som i MainActivity, men privat här så FoundStore kan räkna själv. */
|
||||
private fun distanceMeters(aLat: Double, aLon: Double, bLat: Double, bLon: Double): Double {
|
||||
val R = 6371000.0
|
||||
val dLat = Math.toRadians(bLat - aLat)
|
||||
val dLon = Math.toRadians(bLon - aLon)
|
||||
val s1 = Math.sin(dLat / 2)
|
||||
val s2 = Math.sin(dLon / 2)
|
||||
val aa = s1 * s1 + Math.cos(Math.toRadians(aLat)) * Math.cos(Math.toRadians(bLat)) * s2 * s2
|
||||
val c = 2 * Math.atan2(Math.sqrt(aa), Math.sqrt(1 - aa))
|
||||
return R * c
|
||||
}
|
||||
|
||||
/** Tar bort ett enskilt fynd (utan att röra övriga). */
|
||||
@Synchronized
|
||||
fun remove(ctx: Context, layer: String, id: String) {
|
||||
|
||||
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