gpt4 book ai didi

java - 使用纬度经度计算两点之间的距离?

转载 作者:IT老高 更新时间:2023-10-28 11:51:10 29 4
gpt4 key购买 nike

这是我的尝试,这只是我的代码片段:

final double RADIUS = 6371.01;
double temp = Math.cos(Math.toRadians(latA))
* Math.cos(Math.toRadians(latB))
* Math.cos(Math.toRadians((latB) - (latA)))
+ Math.sin(Math.toRadians(latA))
* Math.sin(Math.toRadians(latB));
return temp * RADIUS * Math.PI / 180;

我正在使用这个公式来获取纬度和经度:

x = Deg + (Min + Sec / 60) / 60)

最佳答案

上面 Dommer 给出的 Java 代码给出了稍微不正确的结果,但是如果您正在处理 GPS 轨迹,那么这些小错误就会加起来。这是 Java 中 Haversine 方法的实现,它还考虑了两点之间的高度差。

/**
* Calculate distance between two points in latitude and longitude taking
* into account height difference. If you are not interested in height
* difference pass 0.0. Uses Haversine method as its base.
*
* lat1, lon1 Start point lat2, lon2 End point el1 Start altitude in meters
* el2 End altitude in meters
* @returns Distance in Meters
*/
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);
}

关于java - 使用纬度经度计算两点之间的距离?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3694380/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com