3

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;

    }
MrPencil
  • 934
  • 8
  • 17
  • 36
  • 2
    `lat2=y.getLatitude()` ... – rpax May 04 '15 at 20:16
  • As @rpax noted you are getting the longitude and storing in in the latitude. – gfelisberto May 04 '15 at 20:30
  • gfelisberto: ok thnaks in which unity is the result being calculated? How I can get it in meter? – MrPencil May 04 '15 at 20:36
  • The units of the result are the units you specify R in, at the moment km. The haversin is overkill for finding whether two points are within 5m though – Pete Kirkham May 04 '15 at 20:42
  • There is lots of existing code to do this:- http://stackoverflow.com/questions/3694380/calculating-distance-between-two-points-using-latitude-longitude-what-am-i-doi/16794680#16794680 – David George May 05 '15 at 09:13

1 Answers1

0

Haversine is overkill for this. Pythagoras is adequate.

Below is javascript function to return distances in metres. I am 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;
}
david strachan
  • 7,174
  • 2
  • 23
  • 33
  • With havsen formula I am getting value of 4-15 meter when I pick two close fixes but with `Pythagoras ` I am getting value like `1.1979775925924383E7, 1.1979777593853703E7, 1.1979940093453636E7` what does these values mean? I getting these values when I pick far points too? – MrPencil May 05 '15 at 16:30
  • For distances less than 10 meters the 5th decimal place of lat/lng coordinates is significant.[**See**](http://en.wikipedia.org/wiki/Decimal_degrees). – david strachan May 05 '15 at 18:51
  • does that mean I have to cosider only the 5th decimal place in the result `d` of the `pyth()` function or I have to pass the 5th decimal place of the longitude and latitude to the function? – MrPencil May 05 '15 at 20:42
  • @MrPencil see corrected formula had + instead of - . The 5th decimal place refers to geographic coordinates. You can round d to whatever you think is required. – david strachan May 14 '15 at 09:18