codepath / codepath/android_guides

Location Geocoder API

Open
#142 0 comments 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
No language data
Stars
28.3k
Forks
6.2k
PR merge metrics
No merged PRs in 30d

Description

Note: These need to be run [inside an AsyncTask](http://guides.codepath.com/android/Creating-and-Executing-Async-Tasks)!
### Reverse Geocoding:

Turn Address in LatLng:

``` java
// getLocationFromAddress(this, "171 Lopez Ave, Menlo Park, CA");
public LatLng getLocationFromAddress(Context context,String strAddress) {
Geocoder coder = new Geocoder(context);
List

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](http://guides.codepath.com/android/Creating-and-Executing-Async-Tasks)!
### Geocoder

Turn Lat and Lng into Address:

``` java
public String getAddressFromLatLng(float lat, float lng) {
Geocoder geocoder;
List

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](http://developer.android.com/training/location/display-address.html)

Note: This need to be run [inside an AsyncTask](http://guides.codepath.com/android/Creating-and-Executing-Async-Tasks)!
### 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,

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.