【发布时间】:2013-11-11 23:15:25
【问题描述】:
Android API 有Location.distanceBetween(),它接受两个纬度/经度值并返回以米为单位的距离。有没有一种方法可以让我只用一个邮政编码来获得这个距离?
【问题讨论】:
-
您可以使用 Google Maps API 获取邮政编码的纬度/经度。它是邮政编码 IIRC 的地理中心。
Android API 有Location.distanceBetween(),它接受两个纬度/经度值并返回以米为单位的距离。有没有一种方法可以让我只用一个邮政编码来获得这个距离?
【问题讨论】:
您可能想使用 Android 的 Geocoder API。像这样的东西应该可以工作:
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();
}
因此,您需要包含国家/地区名称:Get latitude and longitude based on zip using Geocoder class in Android
【讨论】:
Geocoder 可能有点不稳定。我经常超时。 This SO answer 讨论解决方案。我实际上只是填充一个数据库表,因为这是非常静态的数据。