在 Android 中计算罗盘方位/航向位置
我想在谷歌地图视图上的我的位置显示一个箭头,该箭头显示我相对于目的地位置的方向(而不是北)。
a) 我使用磁力计和加速度计的传感器值计算了北向。我知道这是正确的,因为它与 Google 地图视图上使用的指南针一致。
b) 我使用 myLocation.bearingTo(destLocation) 计算了从我的位置到目的地位置的初始方位;
我错过了最后一步;根据这两个值(a 和 b),我可以使用什么公式来获取手机相对于目标位置的指向方向?
感谢对混乱头脑的任何帮助!
I want to display an arrow at my location on a google map view that displays my direction relative to a destination location (instead of north).
a) I have calculated north using the sensor values from the magnetometer and accelerometer. I know this is correct because it lines up with the compass used on the Google Map view.
b) I have calculated the initial bearing from my location to the destination location by using myLocation.bearingTo(destLocation);
I'm missing the last step; from these two values (a & b) what formula do I use to get the direction in which the phone is pointing relative to the destination location?
Appreciate any help for an addled mind!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
好吧,我明白了。对于尝试执行此操作的其他人,您需要:
a) 航向:硬件罗盘的航向。这是以磁北以东的度数
b) 方位角:从您所在位置到目的地位置的方位角。这是真北偏东的度数。
c) 磁偏角:真北和磁北之间的差异
从磁力计 + 加速度计返回的航向以真(磁)北(-180 到 +180)以东的度数表示,因此您需要获得北和磁北之间的差异北为您的位置。这种差异取决于您在地球上的位置。您可以通过使用 GeotropicField 类来获取。
有了这些,您就可以计算在地图上绘制的箭头的角度,以显示您相对于目的地物体的朝向,而不是正北。
首先用赤纬调整您的航向:
其次,您需要将手机面向(航向)的方向从目标目的地而不是正北方向偏移。这是我被困住的部分。从指南针返回的航向值会为您提供一个值,该值描述相对于手机指向位置的磁北位置(以真北以东的度数)。例如,如果值为 -10,您就知道磁北在您左侧 10 度。方位向您提供目的地的角度(以真北以东的度数)。因此,在补偿了赤纬之后,您可以使用下面的公式来获得所需的结果:
然后,您需要将真北以东的度数(-180 到 +180)转换为正常度数(0 到 360):
Ok I figured this out. For anyone else trying to do this you need:
a) heading: your heading from the hardware compass. This is in degrees east of magnetic north
b) bearing: the bearing from your location to the destination location. This is in degrees east of true north.
c) declination: the difference between true north and magnetic north
The heading that is returned from the magnetometer + accelermometer is in degrees east of true (magnetic) north (-180 to +180) so you need to get the difference between north and magnetic north for your location. This difference is variable depending where you are on earth. You can obtain by using GeomagneticField class.
Armed with these you calculate the angle of the arrow to draw on your map to show where you are facing in relation to your destination object rather than true north.
First adjust your heading with the declination:
Second, you need to offset the direction in which the phone is facing (heading) from the target destination rather than true north. This is the part that I got stuck on. The heading value returned from the compass gives you a value that describes where magnetic north is (in degrees east of true north) in relation to where the phone is pointing. So e.g. if the value is -10 you know that magnetic north is 10 degrees to your left. The bearing gives you the angle of your destination in degrees east of true north. So after you've compensated for the declination you can use the formula below to get the desired result:
You'll then want to convert from degrees east of true north (-180 to +180) into normal degrees (0 to 360):
@Damian - 这个想法非常好,我同意答案,但是当我使用你的代码时,我有错误的值,所以我自己写了这个(有人在你的评论中告诉了同样的事情)。我认为用赤纬来计算航向是很好的,但后来我使用了类似的东西:
而不是达米安的代码:
并将-180更改为180为0到360:
然后当你想旋转箭头时,你可以使用这样的代码:
其中
arrowView
是带有箭头图片的ImageView
,postRotate
中的100f参数是pivX和pivY)。我希望我能帮助别人。
@Damian - The idea is very good and I agree with answer, but when I used your code I had wrong values, so I wrote this on my own (somebody told the same in your comments). Counting heading with the declination is good, I think, but later I used something like that:
instead of Damian's code:
and changing -180 to 180 for 0 to 360:
and then when you want to rotate your arrow you can use code like this:
where
arrowView
isImageView
with arrow picture and 100f parameters inpostRotate
is pivX and pivY).I hope I will help somebody.
在此指南针上的箭头显示从您所在位置到Kaaba(目的地位置)的方向,
您可以通过这种方式简单地使用bearingTo。bearing to将为您提供从你的位置到目的地位置
bearingTo会给你一个从-180到180的范围,这会让事情有点混乱。我们需要将此值转换为 0 到 360 的范围以获得正确的旋转。
这是我们真正想要的表格,与bearingTo给我们的相比
,所以我们必须在bearTo之后添加此代码,
您需要实现SensorEventListener及其函数(onSensorChanged,onAcuracyChabge)并将所有代码写入onSensorChanged
完成Qibla 指南针方向的代码在这里
xml 代码在这里
In this an arrow on compass shows the direction from your location to Kaaba(destination Location)
you can simple use bearingTo in this way.bearing to will give you the direct angle from your location to destination location
bearingTo will give you a range from -180 to 180, which will confuse things a bit. We will need to convert this value into a range from 0 to 360 to get the correct rotation.
This is a table of what we really want, comparing to what bearingTo gives us
so we have to add this code after bearTo
you need to implements the SensorEventListener and its functions(onSensorChanged,onAcurracyChabge) and write all the code inside onSensorChanged
Complete code is here for Direction of Qibla compass
xml code is here
我知道这有点老了,但为了像我这样来自谷歌的人,他们在这里没有找到完整的答案。以下是我的应用程序的一些摘录,它们将箭头放在自定义列表视图中......
我愿意打赌它可以被简化,但它有效!
使用 LastKnownLocation 是因为此代码来自 new SimpleCursorAdapter.ViewBinder()
onLocationChanged 包含对 notificationDataSetChanged() 的调用;
代码也来自 new SimpleCursorAdapter.ViewBinder() 来设置图像旋转和列表行颜色(请注意,仅应用于单个列索引)...
如果您想知道我取消了磁传感器戏剧,那么不值得麻烦就我而言。
我希望有人发现这和我通常在 google 将我带到 stackoverflow 时一样有用!
I know this is a little old but for the sake of folks like myself from google who didn't find a complete answer here. Here are some extracts from my app which put the arrows inside a custom listview....
I willing to bet it could be simplified but it works!
LastKnownLocation was used since this code was from new SimpleCursorAdapter.ViewBinder()
onLocationChanged contains a call to notifyDataSetChanged();
code also from new SimpleCursorAdapter.ViewBinder() to set image rotation and listrow colours (only applied in a single columnIndex mind you)...
In case you're wondering I did away with the magnetic sensor dramas, wasn't worth the hassle in my case.
I hope somebody finds this as useful as I usually do when google brings me to stackoverflow!
我不是地图阅读/导航等方面的专家,但肯定“方向”是绝对的而不是相对的,或者实际上,它们是相对于 N 或 S 的,而 N 或 S 本身是固定/绝对的。
示例:假设在您和目的地之间绘制的一条假想线对应于“绝对”SE(相对于磁 N 的方位角为 135 度)。现在假设您的手机指向西北 - 如果您从地平线上的一个假想物体到您的目的地画一条假想线,它将穿过您所在的位置并具有 180 度的角度。现在,指南针意义上的 180 度实际上指的是 S,但目的地不是您手机指向的假想物体的“由于 S”,而且,如果您前往该假想点,您的目的地仍然是东南方你搬到哪里了。
事实上,180 度线实际上告诉您目的地相对于手机(可能还有您)指向的方向位于“您的后方”。
然而,话虽如此,如果计算从假想点到目的地(穿过您的位置)的线的角度以绘制指向目的地的指针是您想要的......只需减去(绝对)方位从想象的物体的绝对方位到目的地并忽略否定(如果存在)。例如,西北 - 东南为 315 - 135 = 180,因此将指针指向屏幕底部,表示“在您身后”。
编辑:我的数学稍有错误...用较大的轴承减去较小的轴承,然后用 360 度减去结果,得到在屏幕上绘制指针的角度。
I'm no expert in map-reading / navigation and so on but surely 'directions' are absolute and not relative or in reality, they are relative to N or S which themselves are fixed/absolute.
Example: Suppose an imaginary line drawn between you and your destination corresponds with 'absolute' SE (a bearing of 135 degrees relative to magnetic N). Now suppose your phone is pointing NW - if you draw an imaginary line from an imaginary object on the horizon to your destination, it will pass through your location and have an angle of 180 degrees. Now 180 degrees in the sense of a compass actually refers to S but the destination is not 'due S' of the imaginary object your phone is pointing at and, moreover, if you travelled to that imaginary point, your destination would still be SE of where you moved to.
In reality, the 180 degree line actually tells you the destination is 'behind you' relative to the way the phone (and presumably you) are pointing.
Having said that, however, if calculating the angle of a line from the imaginary point to your destination (passing through your location) in order to draw a pointer towards your destination is what you want...simply subtract the (absolute) bearing of the destination from the absolute bearing of the imaginary object and ignore a negation (if present). e.g., NW - SE is 315 - 135 = 180 so draw the pointer to point at the bottom of the screen indicating 'behind you'.
EDIT: I got the Maths slightly wrong...subtract the smaller of the bearings from the larger then subtract the result from 360 to get the angle in which to draw the pointer on the screen.
该公式将使用起点到终点的坐标给出方位参见
下面的代码将为您提供方位角(0-360 之间的角度)
这对我有用,希望对其他人也有用
The formula will give the bearing using the coordinates of the start point to the end point see
The following code will give you the bearing (angle between 0-360)
This works for me hope it will work others as well
如果您位于同一时区
将 GPS 转换为 UTM
http://www.ibm.com/developerworks/java/library/j-coordconvert/
http://stackoverflow.com/questions/176137/java-convert-lat-lon-to-utm
UTM 坐标为您提供简单的 XY 2D
计算两个 UTM 位置之间的角度
http://forums.groundspeak.com/GC/ index.php?showtopic=146917
这给出了方向,就好像您在向北看一样
因此,无论您旋转相关的内容,向北只需减去这个角度
如果两个点都有 UTM 45° 角,并且您位于北向东 5°,则您的箭头将指向北纬40度
If you are on the same timezone
Convert GPS to UTM
http://www.ibm.com/developerworks/java/library/j-coordconvert/
http://stackoverflow.com/questions/176137/java-convert-lat-lon-to-utm
UTM coordinates get you a simples X Y 2D
Calculate the angle between both UTM locations
http://forums.groundspeak.com/GC/index.php?showtopic=146917
This gives the direction as if you were looking north
So whatever you rotate related do North just subtract this angle
If both point have a UTM 45º degree angle and you are 5º east of north, your arrow will point to 40º of north
我是这样做的:
Here is how I have done it:
这是从 Google 地图上的位置对象检测方位的最佳方法:->
This is the best way to detect Bearing from Location Object on Google Map:->
我现在正在计算它,但似乎数学取决于你和你的目标相对于真实和磁北在地球上的位置。例如:
有关方位角,请参阅 Sensor.TYPE_ORIENTATION。
请参阅 getDeclination() 了解磁偏角
这假设磁偏角为负(真北以西)并且其方位 >你的轴承。
如果偏角为正且 yourBearing >他们有另一种选择:
我还没有对此进行充分测试,但在纸上玩弄角度让我来到了这里。
I am in the process of figuring it out now but it seems as though the math depends on where you and your target are on the earth relative to true and magnetic North. For example:
See Sensor.TYPE_ORIENTATION for azimuth.
See getDeclination() for declination
This assumes declination is negative (west of true north) and theirBearing > yourBearing.
If declination is positive and yourBearing > theirBearing another option:
I haven't tested this fully but playing with the angles on paper got me here.
术语:真北和磁北之间的差异被称为“变化”而不是磁偏角。指南针读数与磁航向之间的差异称为“偏差”,并且随航向的不同而变化。罗盘摆动可识别设备错误,并允许在设备内置校正的情况下进行校正。磁罗盘将有一个偏差卡,用于描述任何航向的设备错误。
赤纬:天文导航中使用的术语:赤纬就像纬度。它报告恒星距天赤道的距离。要找到恒星的赤纬,请沿着从恒星“直线向下”到天赤道的一个小时圈。沿着小时圈从恒星到天赤道的角度就是恒星的赤纬。
Terminology : The difference between TRUE north and Magnetic North is known as "variation" not declination. The difference between what your compass reads and the magnetic heading is known as "deviation" and varies with heading. A compass swing identifies device errors and allows corrections to be applied if the device has correction built in. A magnetic compass will have a deviation card which describes the device error on any heading.
Declination : A term used in Astro navigation : Declination is like latitude. It reports how far a star is from the celestial equator. To find the declination of a star follow an hour circle "straight down" from the star to the celestial equator. The angle from the star to the celestial equator along the hour circle is the star's declination.