Author: Adam Evyčędo <git@apiote.xyz>
geocode short OLC
app/src/main/java/xyz/apiote/bimba/czwek/dashboard/MainActivity.kt | 26
diff --git a/app/src/main/java/xyz/apiote/bimba/czwek/dashboard/MainActivity.kt b/app/src/main/java/xyz/apiote/bimba/czwek/dashboard/MainActivity.kt index b12c34a0e996cefb965993b6374e59bed1b73f99..7ec5dde6fb6c598c55b32452aadb4de2f68380f4 100644 --- a/app/src/main/java/xyz/apiote/bimba/czwek/dashboard/MainActivity.kt +++ b/app/src/main/java/xyz/apiote/bimba/czwek/dashboard/MainActivity.kt @@ -7,6 +7,9 @@ import android.Manifest import android.content.Intent import android.content.pm.PackageManager +import android.location.Address +import android.location.Geocoder +import android.os.Build import android.os.Bundle import android.view.View import android.widget.Toast @@ -210,9 +213,32 @@ return } val area = olc.decode() showResults(olc.code, area.centerLatitude, area.centerLongitude) + } else if (text.toString().contains(",") && OpenLocationCode.isValidCode(text.toString().split(",").last().trim()) && Geocoder.isPresent()) { + val split = text.toString().split(",") + val code = split.last().trim() + val freePart = split.dropLast(1).joinToString(",") + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + Geocoder(this).getFromLocationName(freePart, 1) { + useShortOlc(it, code, text) + } + } else { + @Suppress("DEPRECATION") + val addresses = Geocoder(this).getFromLocationName(freePart, 1) + useShortOlc(addresses, code, text) + } } else { showResults(ResultsActivity.Mode.MODE_SEARCH, text.toString()) } + } + + private fun useShortOlc(addresses: List<Address>?, code: String, text: CharSequence?) { + if (addresses.isNullOrEmpty()) { + showResults(ResultsActivity.Mode.MODE_SEARCH, text.toString()) + return + } + + val area = OpenLocationCode(code).recover(addresses[0].latitude, addresses[0].longitude).decode() + showResults(text.toString(), area.centerLatitude, area.centerLongitude) } private fun showResults(query: String, centerLatitude: Double, centerLongitude: Double) {