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
|
||||
@@ -371,16 +372,4 @@ private fun openWalkingDirections(ctx: Context, lat: Double?, lon: Double?, titl
|
||||
} catch (_: Exception) {
|
||||
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,23 +167,9 @@ 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). */
|
||||
/** Tar bort ett enskilt fynd (utan att röra övriga). */
|
||||
@Synchronized
|
||||
fun remove(ctx: Context, layer: String, id: String) {
|
||||
val p = prefs(ctx)
|
||||
|
||||
Reference in New Issue
Block a user