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

@@ -17,6 +17,7 @@ import app.klottr.net.NoteDTO
import app.klottr.util.FoundStore import app.klottr.util.FoundStore
import app.klottr.util.LastLocationStore import app.klottr.util.LastLocationStore
import app.klottr.util.SettingsStore import app.klottr.util.SettingsStore
import app.klottr.util.distanceMeters
import com.google.android.gms.location.LocationCallback import com.google.android.gms.location.LocationCallback
import com.google.android.gms.location.LocationRequest import com.google.android.gms.location.LocationRequest
import com.google.android.gms.location.LocationResult import com.google.android.gms.location.LocationResult
@@ -325,16 +326,3 @@ class LocationService : Service() {
.build() .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
}

View File

@@ -116,6 +116,7 @@ import app.klottr.util.SettingsStore
import app.klottr.util.FoundStore.LAYER_DM import app.klottr.util.FoundStore.LAYER_DM
import app.klottr.util.FoundStore.LAYER_KLOTTER import app.klottr.util.FoundStore.LAYER_KLOTTER
import app.klottr.util.FoundStore.LAYER_WIKI import app.klottr.util.FoundStore.LAYER_WIKI
import app.klottr.util.distanceMeters
import app.klottr.util.formatDistance import app.klottr.util.formatDistance
import app.klottr.util.formatRelativeOrTime import app.klottr.util.formatRelativeOrTime
import app.klottr.util.formatCoords import app.klottr.util.formatCoords
@@ -2890,17 +2891,6 @@ fun hasBgLocationPerm(ctx: Context): Boolean {
) == PackageManager.PERMISSION_GRANTED ) == 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? { private fun fetchBitmapFromUrl(url: String): Bitmap? {
return try { return try {
val conn = java.net.URL(url).openConnection() as java.net.HttpURLConnection val conn = java.net.URL(url).openConnection() as java.net.HttpURLConnection

View File

@@ -28,6 +28,7 @@ import app.klottr.net.Wikipedia
import app.klottr.net.WikipediaPage import app.klottr.net.WikipediaPage
import app.klottr.util.Config import app.klottr.util.Config
import app.klottr.util.SettingsStore 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.LocationServices as GmsLocationServices
import com.google.android.gms.location.Priority import com.google.android.gms.location.Priority
import com.google.android.gms.maps.model.CameraPosition import com.google.android.gms.maps.model.CameraPosition
@@ -371,16 +372,4 @@ private fun openWalkingDirections(ctx: Context, lat: Double?, lon: Double?, titl
} catch (_: Exception) { } catch (_: Exception) {
openInMaps(ctx, lat, lon, title) 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
}

View File

@@ -1,5 +1,5 @@
package app.klottr.util package app.klottr.util
import app.klottr.util.distanceMeters
import android.content.Context import android.content.Context
import org.json.JSONObject import org.json.JSONObject
@@ -167,23 +167,9 @@ object FoundStore {
touched = true touched = true
} }
} }
if (touched) e.apply() if (touched) e.apply()
} }
/** Tar bort ett enskilt fynd (utan att röra övriga). */
/** 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 @Synchronized
fun remove(ctx: Context, layer: String, id: String) { fun remove(ctx: Context, layer: String, id: String) {
val p = prefs(ctx) val p = prefs(ctx)

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
}