gpt4 book ai didi

java - 距离小于5米的半正矢公式

转载 作者:行者123 更新时间:2023-12-02 12:19:22 26 4
gpt4 key购买 nike

我正在尝试使用haversin公式来防止更新mysql表。我正在实现一个基于众包的应用程序,其中数据mac、route、lat、long由公交车上的乘客设备记录并发送到服务器。

我的数据库表的 MAC 地址为 UNIQUE KEY。因此,为了避免同一辆公交车上的其他乘客将其数据存储在表中,我会使用 Haversin 公式 过滤这些请求,但我尝试使用彼此相距大约 20 米的两个点但我得到了 4803.800129810092

的号码
//calculate the distance between the request's sender and all other request in the ArrayList. 

private double haversineDistance(LatLong x, LatLong y) {
final int R = 6371; // Radious of the earth
double xLat = x.getLatitude();
double xLon = x.getLongitude();
double yLat = y.getLongitude();
double yLon = y.getLongitude();
;
double latDistance = toRad(yLat - xLat);
double lonDistance = toRad(yLon - xLon);
double a = Math.sin(latDistance / 2) * Math.sin(latDistance / 2)
+ Math.cos(toRad(xLat)) * Math.cos(toRad(yLat))
* Math.sin(lonDistance / 2) * Math.sin(lonDistance / 2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
double distance = R * c;

System.out.println("The distance between two lat and long is:" + distance);

return distance;

}

最佳答案

Haversine 对此来说太过分了。毕达哥拉斯就足够了。

下面是返回以米为单位的距离的 JavaScript 函数。我相信你可以转换为 Java'

function Pyth(lat1,lat2,lng1,lng2){
x = toRad(lng2-lng1) ;
y = toRad(lat2-lat1);
R = 6371000; // gives d in metres
d = sqrt(x*x + y*y) * R;
return d;
}

关于java - 距离小于5米的半正矢公式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30038081/

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