support int based lat/long

for https://github.com/meshtastic/Meshtastic-device/issues/124
This commit is contained in:
geeksville 2020-05-04 08:05:59 -07:00
parent 913a0b56fd
commit 12f7e9a8ce
3 changed files with 28 additions and 20 deletions

View file

@ -79,8 +79,26 @@ data class Position(
val latitude: Double,
val longitude: Double,
val altitude: Int,
val time: Int = (System.currentTimeMillis() / 1000).toInt() // default to current time in secs
val time: Int = currentTime() // default to current time in secs
) : Parcelable {
companion object {
/// Convert to a double representation of degrees
fun degD(i: Int) = i * 1e-7
fun degI(d: Double) = (d * 1e7).toInt()
fun currentTime() = (System.currentTimeMillis() / 1000).toInt()
}
/** Create our model object from a protobuf. If time is unspecified in the protobuf, the provided default time will be used.
*/
constructor(p: MeshProtos.Position, defaultTime: Int = currentTime()) : this(
// We prefer the int version of lat/lon but if not available use the depreciated legacy version
if (p.latitudeI == 0) p.latitudeD else degD(p.latitudeI),
if (p.longitudeI == 0) p.longitudeD else degD(p.longitudeI),
p.altitude,
if (p.time != 0) p.time else defaultTime
)
/// @return distance in meters to some other node (or null if unknown)
fun distance(o: Position) = latLongToMeter(latitude, longitude, o.latitude, o.longitude)