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

佐手、 提交于 2019-12-10 09:34:12

问题


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!


回答1:


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.




回答2:


Your distance algorithm is wrong. It doesn't account for the curvature of the Earth- lines of longitude are closer at higher latitudes, further at lower ones. Use the Location.distanceTo function.



来源:https://stackoverflow.com/questions/14804360/calculate-the-distance-from-point-a-to-line-segment-using-lat-lon

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!