且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用纬度/经度计算从点A到线段的距离

更新时间:2023-10-29 12:52:04

我认为您使用的公式找到2个地理点之间的距离太简单了。由于地球的曲率,公式有点复杂。
看看这里,它提供了一个更准确的近似值。

也有一个Javascript例子,所以你可以通过一些语法调整轻松地将它改为Java。




I am working on an Android app that uses the GPS. I would like to know if there is a way I can throw out GPS Location data if the "new location" (point C) is too far away from line segment AB. 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 have not done this correctly or with enough information. Thanks for your help, I love this site!

I think the formula you are using to find the distance between 2 geographic points is too simplistic. Due to the curvature of the earth, the formula is a bit more complicated. Have a look here, this provides a more accurate approximation.

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

After clarification, you seem to be looking for the distance of a point to a path on the earth between two locations. Check out the cross-track distance variation in the link above.