Open
Description
Note: These need to be run inside an AsyncTask!
Reverse Geocoding:
Turn Address in LatLng:
// getLocationFromAddress(this, "171 Lopez Ave, Menlo Park, CA");
public LatLng getLocationFromAddress(Context context,String strAddress) {
Geocoder coder = new Geocoder(context);
List<Address> address;
LatLng p1 = null;
try {
address = coder.getFromLocationName(strAddress, 5);
if (address == null) { return null; }
Address location = address.get(0);
location.getLatitude();
location.getLongitude();
p1 = new LatLng(location.getLatitude(), location.getLongitude() );
} catch (Exception ex) {
ex.printStackTrace();
}
return p1;
}
Source: http://stackoverflow.com/a/27834110/313399
Note: This need to be run inside an AsyncTask!
Geocoder
Turn Lat and Lng into Address:
public String getAddressFromLatLng(float lat, float lng) {
Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(mContext, Locale.getDefault());
try {
addresses = geocoder.getFromLocation(lat, lng, 1);
String address = addresses.get(0).getAddressLine(0);
String city = addresses.get(0).getAddressLine(1);
String country = addresses.get(0).getAddressLine(2);
return address + "\n" + city + ", " + country;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
Source: Displaying Address Geocoding
Note: This need to be run inside an AsyncTask!
Workarounds
You might try catching this request and retrying the request a few times, it will usually work as long as you have internet. Another approach you might find useful:
- http://stackoverflow.com/questions/16119130/android-java-io-ioexception-service-not-available
- http://stackoverflow.com/questions/19059894/google-geocoder-service-is-unavaliable-coordinates-to-address
- http://stackoverflow.com/questions/22803755/java-io-ioexception-timed-out-waiting-for-response-from-server
In short you may want to try one of the workarounds to avoid using the geocoder SDK and instead use the geocoder API through a manual REST call instead. Hope that helps,