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 (34 loc) · 1.21 KB
/
OwnLocationModel.kt
File metadata and controls
41 lines (34 loc) · 1.21 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.osmdroid.util.GeoPoint
import timber.log.Timber
import javax.inject.Inject
import javax.inject.Singleton
@Singleton
class OwnLocationModel @Inject constructor() {
@JvmField
var ownLocation: GeoPoint? = null
private var isLocationPrecise: Boolean = false
fun setLocation(location: GeoPoint, accuracy: Float) {
ownLocation = location
isLocationPrecise = accuracy < ACCURACY_PRECISE_THRESHOLD
}
fun hasPreciseLocation(): Boolean {
return ownLocation != null && isLocationPrecise
}
fun getLocationJson(): JSONObject {
val locationObject = JSONObject()
try {
val location = ownLocation ?: throw NullPointerException("ownLocation is null")
locationObject.put("longitude", (location.longitude * 1000000.0).toInt().toString())
locationObject.put("latitude", (location.latitude * 1000000.0).toInt().toString())
} catch (e: JSONException) {
Timber.e(e)
}
return locationObject
}
companion object {
private const val ACCURACY_PRECISE_THRESHOLD = 50.0f // meters
}
}