gpt4 book ai didi

java - 计算由纬度、经度表示的两点之间的距离,精度高达 15 英尺

转载 作者:行者123 更新时间:2023-12-02 07:34:37 25 4
gpt4 key购买 nike

我已将提供的公式转换为Java here 。但准确性是一个问题。我们使用 GPS 坐标。

我们使用 iPhone 提供的 GPS 位置,精度高达 10 位小数。

/*
* Latitude and Longitude are in Degree
* Unit Of Measure : 1 = Feet,2 = Kilometer,3 = Miles
*/
//TODO 3 Change Unit of Measure and DISTANCE_IN_FEET constants to Enum
public static Double calculateDistance(double latitudeA,double longitudeA,double latitudeB,double longitudeB,short unitOfMeasure){

Double distance;

distance = DISTANCE_IN_FEET *
Math.acos(

Math.cos(Math.toRadians(latitudeA)) * Math.cos(Math.toRadians(latitudeB))
*
Math.cos(Math.toRadians(longitudeB) - Math.toRadians(longitudeA))
+
Math.sin(Math.toRadians(latitudeA))
*
Math.sin(Math.toRadians(latitudeB))

);

return distance;

}

仅供引用:公共(public)静态最终 int DISTANCE_IN_FEET = 20924640;

然后我使用 Math.round(distance);转换为长整型。

对于实际 25 英尺,我得到 7 英尺的输出。

最佳答案

您需要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;
}

关于java - 计算由纬度、经度表示的两点之间的距离,精度高达 15 英尺,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18861728/

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