Android Kotlin Geocoder DEADLINE_EXCEEDED on Android when getting location coordinates/adresses
Asked Answered
G

1

13

In my android app i am trying to get the address from lat lng using the Geocoder using getFromLocation method and the GeocodeListener but sometimes the app is getting crashed

Crash Log

Caused by java.io.IOException: egma: DEADLINE_EXCEEDED: Deadline CallOptions will be exceeded in 4.959774531s. 
       at android.location.Geocoder$GeocodeListener.getResults(Geocoder.java:246)
       at android.location.Geocoder.getFromLocation(Geocoder.java:134)

Code

geocoder.getFromLocation(
  latLng.latitude,
  latLng.longitude,
  1,
  object : Geocoder.GeocodeListener {
            override fun onGeocode(addresses: MutableList<Address>) {
              val addressLine = addresses.first().getAddressLine(0)
                   onAddressResult(addressLine, null) // callback
             }

             override fun onError(errorMessage: String?) {
               super.onError(errorMessage)
                  onAddressResult(null, errorMessage)
             }
  },
)

What is the reason for the crash. how can i avoid it?

Gaylor answered 22/2, 2023 at 15:4 Comment(2)
How did you solve this problem? I am getting the same issue, in my live production application. And I am not able to find the exact solution to this problem. Can you please help?Sidell
@RoopKishore used web version developers.google.com/maps/documentation/geocoding/… .. refer link.Gaylor
I
0

In Kotlin, you can use coroutines to perform network operations asynchronously so that the main thread doesn't block, I solved it like this

      addressesname = addressesname(lat, lng, geocoder)

      private suspend fun addressesname(lat: Double, lng: Double,geocoder: Geocoder) = withContext(Dispatchers.IO) {
         var addressesname = ""
         try {
              // For Android SDK < 33, the addresses list will be still obtained from the getFromLocation() method
              val addresses = geocoder.getFromLocation(lat, lng, 1) as MutableList<Address>
              addressesname = addresses[0].getAddressLine(0)

         }catch (e :Exception){
              showErrorSnackBar("Tienes latencia, revisa tus datos moviles")
         }
         addressesname
      }
Interstadial answered 24/1 at 1:5 Comment(2)
I think, this crash is not related to threading. It's internal crash within geocoding library possibly related to it's request scheduling mechanism.Cowart
This is merely the way to use Geocoder on older API. Using a thread lets the program continue, but does not solve the issue. Moreover, from my experience when DEADLINE_EXCEEDED is going to happen then also attempts to start new requests on new threads will block until the one with DEADLINE_EXCEEDED throws this exception.Adeliaadelice

© 2022 - 2024 — McMap. All rights reserved.