用地理坐标(纬度、经度)计算物体的方向
我有一个玩家和另一个物体的坐标。两者都带有地理坐标(纬度和经度)。我还有玩家所面对的方向(指南针)。如何计算从玩家方向到另一个物体的角度?例如,我想知道该物体是否位于玩家的右侧/左侧以及多少度。
多谢!
I have the coordinates of a player and another object. Both are with geographical coordinates (Latitude and longitude). I have also the direction in what the player is facing (compass). How can I calculate the angle to the other object from the player direction? e.g. I want to know if the object is to the right/left of the player and how many degrees.
Thanks a lot!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有一些这取决于,答案本质上是,您想知道如何做
对于短距离(<10公里),您可以忽略地球的曲率,并将其视为二维笛卡尔地图(纬度/经度为 XY)。那么你的问题就变成了基本的三角学。
为了获得更大的距离或提高精度,您可以使用地球的球体模型进行近似(< em>假设地球是一个完美的球体,但事实并非如此)并计算伟大圆
方位
和距离
。或者,您可以将地球建模为椭圆体,并计算其地理导航。
两个可能有帮助的网页:使用纬度/经度坐标计算距离的详细信息和计算纬度/经度点之间的距离、方位等。
注意:atan2 和 Haversine 公式 通常是有用的实现细节。
小补充说明:在此上下文中,
bearing
是heading
或direction
的同义词。With a few it depends, the answer is in essence, you want to know about how to do geographic navigation. One of the reasons it depends is that the distances involve as well as the accuracy needed may influence the answer.
For short distances (<10km) you may be able to ignore the curvature of the Earth, and treat it like a two dimensional Cartesian map (latitude / longitude as X-Y). Then you question becomes basic trigonometry.
For larger distances, or improved accuracy, you can either approximate using an spheroid model of the Earth (assume the Earth is a perfect sphere, which it is not) and calculate the Great Circle
bearing
anddistance
.Or you can model the Earth as an ellipsoid, and calculate its geographic navigation.
Two web pages that may help: Details for computing distance using lat/long coordinates and Calculate distance, bearing and more between Latitude/Longitude points.
Note: atan2 and Haversine formula are often useful implementation details.
Small added note:
bearing
is a synonym forheading
ordirection
in this context.您需要这个球面三角公式:http://williams.best.vwh.net/avform。 htm#Crs 一旦确定了航向(相对于正北的角度),您可以减去玩家所面向方向的罗盘航向以获得相对航向。
(我不知道 Android 是否会自动补偿磁性变化,但如果没有,您也必须考虑到它才能在所有区域获得正确的角度)
You need this spherical trig formula: http://williams.best.vwh.net/avform.htm#Crs Once you have the course (angle relative to true north), you can subtract off the compass heading of the direction the player is facing to get the relative heading.
(I don't know if Android automatically compensates for magnetic variation or not, but if not you'll have to account for it too to get the angle right in all areas)
API 中有一些工具可以为您执行此操作: Location.bearingTo(Location) 和 GeomagicSensor会给您可以确定从您的位置到目标的方向 - 然后您可以根据设备当前的航向进行调整。
如果您已经运行了 MapView &如果比较懒的话,可以设置一个 MyLocationOverlay、enableCompass并跳过地磁传感器并让 MapView 为您完成。
There are tools in the API to do this for you: Location.bearingTo(Location) and GeomagneticSensor will give you the direction from your position to the target - which you can then adjust based off the devices current heading.
If you've already got a MapView running & are lazy, set up a MyLocationOverlay, enableCompass and skip the GeomagneticSensor and let the MapView do it for you.