Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Geocoder - getFromLocation() deprecated

I've received a message that this function (or it's constructor) has been deprecated. There's a new constructor of that function that accepts an additional parameter 'Geocoder.GeocodeListener listener' but that new constructor requires an API Level 33 and above. What should I do for the lower API levels, what's the solution?

enter image description here

like image 941
Hassa Avatar asked Feb 04 '26 20:02

Hassa


1 Answers

The Official Doc - Use getFromLocation(double, double, int, android.location.Geocoder.GeocodeListener) instead to avoid blocking a thread waiting for results.

Example:

//Variables
val local = Locale("en_us", "United States")
val geocoder = Geocoder(this, local)
val latitude = 18.185600
val longitude = 76.041702
val maxResult = 1


//Fetch address from location
geocoder.getFromLocation(latitude,longitude,maxResult,object : Geocoder.GeocodeListener{
 override fun onGeocode(addresses: MutableList<Address>) {

    // code                      
 }
 override fun onError(errorMessage: String?) {
     super.onError(errorMessage)

 }

})
like image 177
Sachin Avatar answered Feb 06 '26 08:02

Sachin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!