我正在尝试使用hasrsin公式来防止更新mysql表。我正在实施一个基于众包的应用程序,其中数据mac, route, lat, long
由passangers im bus的设备记录并发送到服务器。
我的数据库表的mac地址为UNIQUE KEY
。因此,为了避免同一总线中的其他乘客将数据存储在表格中,我会使用Haversin formula
来过滤这些请求,但我尝试使用牵引点,它们彼此相距大约20米,但我是gettig号码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;
}
答案 0 :(得分:0)
Haversine对此有些过分。毕达哥拉斯就足够了。
以下是javascript函数以米为单位返回距离。我相信你可以转换为Java'
PREPARE qry (text, integer , integer) AS
SELECT *
FROM employee
WHERE name = $1
AND age = $2
AND ( $3 IS NULL OR salary = $3)
;
-- and call the prepared statement:
EXECUTE qry ('Alice', 13, 3);
EXECUTE qry ('Alice', 13, NULL);