0

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?

theblang
  • 10,215
  • 9
  • 69
  • 120

1 Answers1

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

Community
  • 1
  • 1
jlhonora
  • 10,179
  • 10
  • 46
  • 70
  • 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