ref: f7001336c32940112f2229735bd2b5135826b8ba
app/src/main/java/xyz/apiote/bimba/czwek/repo/Position.kt
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 42 43 44 45 46 47 48 49 50 51 52 53 54 |
// SPDX-FileCopyrightText: Adam Evyčędo // // SPDX-License-Identifier: GPL-3.0-or-later package xyz.apiote.bimba.czwek.repo import android.location.Location import android.os.Parcelable import kotlinx.parcelize.Parcelize import xyz.apiote.bimba.czwek.api.PositionV1 @Parcelize data class Position(val latitude: Double, val longitude: Double): Parcelable { init { if (latitude > 90 || latitude < -90 || longitude > 180 || longitude < -180) { throw IllegalArgumentException("$latitude must be [-90, 90], $longitude must be [-180, 180]") } } constructor(p: PositionV1) : this(p.latitude, p.longitude) fun isZero(): Boolean { return latitude == 0.0 && longitude == 0.0 } fun distanceTo(other: Position): Float { return Location(null).apply { latitude = this@Position.latitude longitude = this@Position.longitude }.distanceTo(Location(null).apply { latitude = other.latitude longitude = other.longitude }) } override fun toString(): String { return "$latitude,$longitude" } companion object { fun comparator(centre: Position) = object : Comparator<Position> { override fun compare( o1: Position?, o2: Position? ): Int { if (o1 == null || o2 == null) { throw NullPointerException() } return o1.distanceTo(centre).compareTo(o2.distanceTo(centre)) } } } } |