且构网

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

iPhone 指南针 GPS 方向

更新时间:2023-02-24 13:27:37

有一个标准的航向"或方位"方程可以使用——如果你在 lat1,lon1,你感兴趣的点是在 lat2,lon2,则方程为:

There is a standard "heading" or "bearing" equation that you can use - if you are at lat1,lon1, and the point you are interested in is at lat2,lon2, then the equation is:

heading = atan2( sin(lon2-lon1)*cos(lat2), cos(lat1)*sin(lat2) - sin(lat1)*cos(lat2)*cos(lon2-lon1))

这为您提供了以弧度为单位的方位,您可以通过乘以 180/π 将其转换为度数.然后该值介于 -180 和 180 度之间,因此要获得标准指南针方位,请在任何否定答案上加上 360.

This gives you a bearing in radians, which you can convert to degrees by multiplying by 180/π. The value is then between -180 and 180 degrees, so to get a standard compass bearing add 360 to any negative answers.

atan2 是一个与 arctan 相关的标准函数,与您所在的位置相比,它对您的目标点可能位于的四个可能的象限执行正确的操作.

atan2 is a standard function related to arctan, that does the right thing for the four possible quadrants that your destination point could be in compared to where you are.