26

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.

xomena
  • 27,383
  • 5
  • 77
  • 109
fargath
  • 7,376
  • 6
  • 22
  • 35
  • Try this, by using Geocoder as a service: https://developer.android.com/training/location/display-address#java – Reejesh PK Sep 19 '18 at 09:57

9 Answers9

16

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!

xomena
  • 27,383
  • 5
  • 77
  • 109
  • Using Geocoder as a service Works! See here for more: https://developer.android.com/training/location/display-address#java – Reejesh PK Sep 19 '18 at 09:56
  • 4
    A year and a half later and the status of both of those bugs is "New". Thanks Google. – nasch Feb 02 '19 at 18:06
  • 2
    Lol.. Just putting that call in a background thread or Coroutine works – Mohanakrrishna Jun 21 '19 at 05:13
  • @ReejeshPK your link is not pointing to the correct page anymore (no notion of Geocoder, display address or anything of that kind. Could you share your solution? – Rule Jun 09 '20 at 08:07
  • 1
    @Rule, just run your geocoder in a background service or async task, It is not recommended I guess, it gives the result 90% of times, so in case you didnt get the result, go for API : https://developers.google.com/maps/documentation/geocoding/start – Reejesh PK Jun 09 '20 at 10:32
10

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

Pxaml
  • 651
  • 1
  • 10
  • 27
  • how to do that ? – Siddarth G May 03 '18 at 13:08
  • try { var connection = await internet.CheckConnection(); var Searchbar = (SearchBar)sender; /* This will prevent GRCP Java failed */ if (!connection.IsSuccess) { await dialogService.ShowMessage("No address found", "Please make sure you are not offline."); return; }catch(ex){ get the exeption .... } – Pxaml May 03 '18 at 13:36
2

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

2

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.

hiddeneyes02
  • 1,628
  • 1
  • 22
  • 42
2

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
 }

}

lasec0203
  • 1,784
  • 16
  • 32
2

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()
M. Usman Khan
  • 4,258
  • 1
  • 48
  • 62
2

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

Furkan
  • 21
  • 2
0

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.

0

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

arush436
  • 1,304
  • 15
  • 17