forked from criticalmaps/criticalmaps-android
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOwnLocationModel.kt
More file actions
41 lines (33 loc) · 1.23 KB
/
OwnLocationModel.kt
File metadata and controls
41 lines (33 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package de.stephanlindauer.criticalmaps.model
import org.json.JSONException
import org.json.JSONObject
import org.maplibre.android.geometry.LatLng
import timber.log.Timber
import javax.inject.Inject
import javax.inject.Singleton
@Singleton
class OwnLocationModel @Inject constructor() {
@JvmField
var ownLocation: LatLng? = null
private var isLocationPrecise = false
fun setLocation(location: LatLng, accuracy: Float) {
ownLocation = location
isLocationPrecise = accuracy < ACCURACY_PRECISE_THRESHOLD
}
fun hasPreciseLocation(): Boolean = ownLocation != null && isLocationPrecise
fun getLocationJson(): JSONObject {
val location = ownLocation
require(location != null) { "Location must be set before calling getLocationJson()" }
val locationObject = JSONObject()
try {
locationObject.put("longitude", (location.longitude * 1_000_000.0).toInt().toString())
locationObject.put("latitude", (location.latitude * 1_000_000.0).toInt().toString())
} catch (e: JSONException) {
Timber.e(e)
}
return locationObject
}
companion object {
private const val ACCURACY_PRECISE_THRESHOLD = 50.0f // meters
}
}