Java 使用经度计算两点之间的距离?

Java 使用经度计算两点之间的距离?,第1张

Java 使用经度计算两点之间的距离?

上面Dommer给出的Java代码给出的结果略有不正确,但是如果你正在处理GPS轨迹,则小错误加起来。这是Java中Haversine方法的实现,该方法还考虑了两点之间的高度差。

public static double distance(double lat1, double lat2, double lon1,        double lon2, double el1, double el2) {    final int R = 6371; // Radius of the earth    double latDistance = Math.toRadians(lat2 - lat1);    double lonDistance = Math.toRadians(lon2 - lon1);    double a = Math.sin(latDistance / 2) * Math.sin(latDistance / 2) + Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) * Math.sin(lonDistance / 2) * Math.sin(lonDistance / 2);    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));    double distance = R * c * 1000; // convert to meters    double height = el1 - el2;    distance = Math.pow(distance, 2) + Math.pow(height, 2);    return Math.sqrt(distance);}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5146300.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-11-17
下一篇2022-11-17

发表评论

登录后才能评论

评论列表(0条)

    保存