I am trying to use the haversin formula to prevent updating the mysql table. I am implementing an app based on crowdsorucing where the data mac, route, lat, long
is being recorded by the devices of the passangers im bus and send to the server.
My database table has mac address as UNIQUE KEY
. So to avoid that other passanger in the same bus to store their data in the table I would filter these requests by using the Haversin formula
but I tried it with tow point which are roughly 20 meter close to each other but I am gettig number of 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;
}