简体   繁体   中英

Geocoder.getFromLocation grpc failed on Android real device

I need to get the city and state from latitude and longitude. Geocoder does this function to get the city and state. But I am getting an error java.io.IOException: grpc failed in below line only on Android real device . It is working fine in emulators .

new Geocoder(context).getFromLocation(latLng.latitude, latLng.longitude, 1).get(0);

Also I have been calling this function using Asynctask which is suggested in another post Geocoder grpc failed in stack overflow but nothing works for me.

And I have restart the device as well but still it is failing.

Help needed here. Thanks in advance.

Note : I tested in Google Pixel XL, Samsung S6 edge, Samsung S4.

It looks like this is ongoing issue that was reported in the Google issue tracker both for real devices and emulators. You can refer to the following bugs:

https://issuetracker.google.com/issues/64418751

https://issuetracker.google.com/issues/64247769

Unfortunately, Google haven't solved these issues yet.

As a workaround you can consider using the Geocoding API web service. Please note that there is a Java client library for web services that you can find on Github:

https://github.com/googlemaps/google-maps-services-java

Using Java client library for web services you can implement reverse geocoding lookup that shouldn't give you the error that you experience with native Android geocoder.

The Javadoc for client library is located at

https://googlemaps.github.io/google-maps-services-java/v0.2.5/javadoc/

I hope this helps!

Check your internet connection . what is going on is at some point between the search and the results rendering , the connection is either to slow or not working at all . so the default error is no GRCP found. your code needs to handle this type of errors and prevent the crash of the map

Happened for me both with a real and an emulated device. Solved my restarting the app after cleaning the project

For me, it was the device time. I was doing some tests, and I had to change the device time. The device time must be set correctly, otherwise you get this error.

Making the call to fetch an address via geocoder is recommended to do via an IntentService ....but if you're in rapid development mode and don't want to bother setting up a service just yet, you can use a try/catch block

example in Kotlin:

try{
 val addressList = geocoder.getFromLocationName(locationName, 3)
 
 //...

}catch(e: IOException) {

 when{
  e.message == "grpc failed" -> {/* display a Toast or Snackbar instead*/ }
  else -> throw e
 }

}

It is a reported bug in Android, check @xomena answer for the link.

Alternately, you can use the web services. Add this to gradle dependencies:

api 'com.google.maps:google-maps-services:0.10.2'

Sample code to get addresses:

        val geoApiCtx = GeoApiContext.Builder()
            .apiKey(Util.readGoogleApiKey(ctx))
            .build()

        val addresses = GeocodingApi.reverseGeocode(geoApiCtx, loc).await()

When I run with emulator I got the same problem. The problem has solved after i run with real device.

What I did was, I changed my accuracy to GPS only, in this case, the application was not calculating my location. I again changed the accuracy to high accuracy and the application started working, no crash at all. you can change your GPS accuracy from location setting on your android mobile.

Using an emulator. I had to update the Google Play Services on the device and then it worked!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM