Add [lat] [lon] [name] syntax to lookup locations by lat lon

This commit is contained in:
MM20 2022-07-11 18:55:30 +02:00
parent 5bace7d656
commit 5f9ccaf77e
No known key found for this signature in database
GPG Key ID: 0B61A8F2DEAFA389

View File

@ -9,7 +9,6 @@ import de.mm20.launcher2.ktx.putDouble
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.io.IOException
import kotlin.math.abs
import kotlin.math.absoluteValue
import kotlin.math.roundToInt
@ -20,6 +19,15 @@ abstract class LatLonWeatherProvider : WeatherProvider<LatLonWeatherLocation>()
override suspend fun lookupLocation(query: String): List<LatLonWeatherLocation> {
val parts = query.split(" ", limit = 3)
val lat = parts.getOrNull(0)?.toDoubleOrNull()
val lon = parts.getOrNull(1)?.toDoubleOrNull()
if (lat != null && lon != null && lat >= -90 && lat <= 90 && lon >= -180 && lon <= 180) {
val name = parts.getOrElse(2) { getLocationName(lat, lon) }
return listOf(
LatLonWeatherLocation(name, lat, lon)
)
}
if (!Geocoder.isPresent()) return emptyList()
val geocoder = Geocoder(context)
val locations =