从一个坐标到另一个坐标的方位角

2022-01-14 00:00:00 bearing geolocation java

我从 http://www.movable-type.co.uk 实现了轴承"公式/scripts/latlong.html.但这似乎非常不准确 - 我怀疑我的实施中有一些错误.你能帮我找到它吗?我的代码如下:

I implemented the "bearing" formula from http://www.movable-type.co.uk/scripts/latlong.html. But it seems highly inaccurate - I suspect some mistakes in my implementation. Could you help me with finding it? My code is below:

protected static double bearing(double lat1, double lon1, double lat2, double lon2){

double longDiff= lon2-lon1;
double y = Math.sin(longDiff)*Math.cos(lat2);
double x = Math.cos(lat1)*Math.sin(lat2)-Math.sin(lat1)*Math.cos(lat2)*Math.cos(longDiff);

return Math.toDegrees((Math.atan2(y, x))+360)%360;
}

推荐答案

你只是把括号 () 放错地方了.

You just have your parentheses () in the wrong place.

您正在为弧度值添加度数,这不起作用.toDegrees() 将为您完成从弧度到度数的转换,然后一旦您有度数的值,您就可以进行标准化.

You are adding degrees to a value in radians, which won't work. toDegrees() will do the conversion from radians to degrees for you, then you do the normalisation once you have a value in degrees.

你有:

 Math.toDegrees( (Math.atan2(y, x))+360 ) % 360;

但你需要:

( Math.toDegrees(Math.atan2(y, x)) + 360 ) % 360;

还请记住,Math.sin()Math.cos() 和所有其他三角函数的所有输入都必须以弧度表示.如果您的输入是度数,您需要先使用 Math.toRadians() 进行转换.

Remember also that all inputs to Math.sin(), Math.cos() and all the other trigonometric functions must be in radians. If your inputs are degrees you'll need to convert them using Math.toRadians() first.

相关文章