问题
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.
回答1:
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!
回答2:
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
回答3:
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.
回答4:
Happened for me both with a real and an emulated device. Solved my restarting the app after cleaning the project
回答5:
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
}
}
回答6:
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.
来源:https://stackoverflow.com/questions/47331480/geocoder-getfromlocation-grpc-failed-on-android-real-device