1

I want to get the street address from longitude and latitude. Currently I am using geocoder.getFromLocation(latitute,longitude,1)

        fun getLastKnownLocation() {
        fusedLocationClient.lastLocation
            .addOnSuccessListener { location ->
                if (location != null) {
                    longitude.value = location.longitude
                    latitude.value = location.latitude

                    val geocoder = Geocoder(app)

                    if (Build.VERSION.SDK_INT >= 33) {
                        geocoder.getFromLocation(
                            location.latitude,
                            location.longitude,
                            1
                        ) { addresses ->
                            address.value = addresses.first().getAddressLine(0)
                        }
                    } else {
                        try {
                            val addresses = geocoder.getFromLocation(
                                location.latitude,
                                location.longitude,
                                1)?.firstOrNull()
                            address.value = addresses?.getAddressLine(0) ?: "No address found"
                        } catch (Exp: Exception) {
                            address.value = "No address found"
                        }
                    }
                }
            }
    }

But is unstable and does not work sometimes. Geocoder does not get always the address and it takes to long.

Any idea what else I can use or what I should change on the implementation? Have a nice day!

Cipri
  • 57
  • 1
  • 9
  • If you want help with your current implementation, ask a fresh question where you provide a [mcve] and explain in detail what "is unstable and does not work sometimes" means. There is a lot of existing material in [the documentation](https://developer.android.com/training/location) on obtaining your location in terms of latitude and longitude. If your concerns lie more with `Geocoder`, you would need to research geocoding Web services. – CommonsWare Jan 28 '23 at 18:59
  • There's no solution that's 100% for this. All of them require someone to keep an up to date db of lat/lng to address data. You can look for another provider, but nobody is going to always be right. Not to mention addressses aren't exactly well defined- building can have 1, 0, or many addresses so mapping them is in some cases guess work. – Gabe Sechan Jan 28 '23 at 19:01
  • The problem is with geocoder.getFromLocation. Its very unstable and deprecated. What else I can use? – Cipri Jan 28 '23 at 19:28

0 Answers0