The Android API has Location.distanceBetween()
, which accepts two lat/lon values and returns a distance in meters. Is there a way that I could get this distance with only having a zip (postal) code for one of my points?
Asked
Active
Viewed 1,273 times
0

theblang
- 10,215
- 9
- 69
- 120
-
You can use the Google Maps API to get the lat/long for a zip code. It is the geographic center of the zip code IIRC. – Erik Nedwidek Nov 11 '13 at 23:19
1 Answers
1
You may want to use Android's Geocoder API. Something like this should work:
String locationName = zipCode + ", " + countryName;
Geocoder geoCoder = new Geocoder(context, Locale.getDefault());
try {
List<Address> address = geoCoder.getFromLocationName(locationName, 1);
double latitude = address.get(0).getLatitude();
double longitude = address.get(0).getLongitude();
Location.distanceBetween(...);
} catch (IOException e) {
e.printStackTrace();
}
You need to include the country's name because of this: Get latitude and longitude based on zip using Geocoder class in Android
-
It seems that `Geocoder` can be a little flaky. It was timing out for me frequently. [This SO answer](http://stackoverflow.com/a/15236615/1747491) discusses a solution. I am actually going to just populate a database table instead, since this is very static data. – theblang Nov 12 '13 at 16:23