Calculate distance from point A to line segment using Lat / Lon

I am working on an Android application that uses GPS. I would like to know if there is a way to throw out the GPS location data if the "new location" (point C) is too far from the AB segment. I am using the Point to Line Segment formula found on Wikipedia .

So far, the code I have is returning NaN when I try to use latitude and longitude coordinates.

private void verifyGPSLocation(Location start, Location end, Location current){
    final double errorValue = 0.0000216;
    double normalLength = Math.hypot(end.getLatitude() - start.getLatitude(), end.getLongitude() - start.getLongitude());
    double ret = Math.abs(((current.getLatitude() - start.getLatitude()) * (end.getLongitude() - start.getLongitude()) - (end.getLatitude() - start.getLatitude()))/normalLength );
    Log.e("Coooooord", normalLength+"--"+ret);
}

      

This is my first post, so please let me know if I haven't done it right or with enough information. Thanks for your help, I love this site!

+3


source to share


2 answers


I think the formula you use to find the distance between two geographic points is oversimplified. Due to the curvature of the earth, the formula is a little more complicated. Check it out here , it provides a more accurate approximation.

There is also a Javascript example, so you can easily change it to Java with a few syntax tweaks.



After asking, you seem to be looking for a point-to-path distance on the ground between two points. Check out the change in track spacing in the link above.

+2


source


Your distance algorithm is wrong. This does not explain that the curvature of the earth's lines of longitude is closer to higher latitudes, further to lower ones. Use the Location.distanceTo function.



+2


source







All Articles