我的要求类似于this question ,除了我被禁止使用纬度和经度值这一事实。我想计算两个邮政编码之间的“步行” 距离。稍后我还需要查询“谁在 X 公里内?”
我不确定它是否可以实现。真的有可能找到两个给定邮政编码之间的距离吗? 我不想要仅适用于美国/英国邮政编码的答案。我需要一个适用于世界上任何两个邮政编码的通用解决方案。
如果不使用经度和纬度计算距离是不可能的,那么我可以得到给定邮政编码的经度/纬度值吗? (惊人)但是如何?
任何教程/示例都会有很大帮助..因为我正在从事商业项目,所以不能使用 Google Maps API .所以请不要推荐任何其他许可服务。
提前致谢
更新 this question的答案之一建议使用 MySQL或 Postgress ...这对我有用吗...?
最佳答案
如果您有兴趣,这是我对 haversine formula 的 java 实现
/**
* Calculates the distance in km between two lat/long points
* using the haversine formula
*/
public static double haversine(
double lat1, double lng1, double lat2, double lng2) {
int r = 6371; // average radius of the earth in km
double dLat = Math.toRadians(lat2 - lat1);
double dLon = Math.toRadians(lng2 - lng1);
double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2))
* Math.sin(dLon / 2) * Math.sin(dLon / 2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
double d = r * c;
return d;
}
我特此将此捐赠给 GPL 下的公共(public)领域 :)
关于java - 如何使用 Java 代码查找两个邮政编码之间的距离?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7426710/