Java 距离小于5米的哈弗森公式
我试图使用haversin公式来防止更新mysql表。我正在实施一个基于众包的应用程序,其中数据Java 距离小于5米的哈弗森公式,java,haversine,Java,Haversine,我试图使用haversin公式来防止更新mysql表。我正在实施一个基于众包的应用程序,其中数据mac、route、lat、long由乘客im总线的设备记录并发送到服务器 我的数据库表的mac地址为唯一键。因此,为了避免同一辆公交车上的其他乘客将他们的数据存储在表中,我会使用Haversin公式对这些请求进行过滤,但我尝试使用两个彼此接近约20米的点进行过滤,但我得到了4803.800129810092 //calculate the distance between the request's
mac、route、lat、long
由乘客im总线的设备记录并发送到服务器
我的数据库表的mac地址为唯一键
。因此,为了避免同一辆公交车上的其他乘客将他们的数据存储在表中,我会使用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;
}
哈弗森在这方面做得太过分了。毕达哥拉斯已经足够了 下面是返回以米为单位的距离的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;
}
lat2=y.getLatitude()
…正如@rpax所指出的,您正在获取经度并将其存储在纬度中。gfelisberto:ok thnaks计算结果的单位是哪一个?我怎么能用米来表示呢?结果的单位是你指定的单位,在km的时刻。哈弗森公式在判断两点是否在5m范围内有点过分了,尽管有很多现有的代码可以做到这一点:-使用哈弗森公式,当我选择两个接近的固定点时,我得到了4-15米的值,但是使用Pythagoras
我得到了1.1979775925924383E7,1.1979777593853703E7,1.1979940093453636E7
这些值是什么意思?我也在拾取远点时获得这些值?对于小于10米的距离,lat/lng坐标的小数点后第5位很重要。。这是否意味着我必须在pyth()的结果d
中仅对小数点后第5位进行排序
函数或我必须将经度和纬度的小数点后第5位传递给函数?@MrPencil请参阅更正的公式had+,而不是-。小数点后第5位为地理坐标。你可以把d四舍五入到你认为需要的任何地方。