Haversine's formula for distance less than 5 meters
I am trying to use haversin formula to prevent mysql table from being updated. I am implementing a reusable based application where data is mac, route, lat, long
written by passbus devices im bus and sent to server.
My database table has mac address as UNIQUE KEY
. So to avoid another passenger on the same bus storing their data in the table, I would filter these queries with Haversin formula
, but I tried it with a tow point that is about 20 meters close together, but I get quantity4803.800129810092
//calculate the distance between the request 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;
}
source to share
Haversine is too much for that. Pythagoras is adequate.
Below is a javascript function to return distances in meters. I'm sure you can convert to 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; }
source to share