计算罗盘方位/标题位置的Andr​​oid [英] Calculate compass bearing / heading to location in Android

查看:128
本文介绍了计算罗盘方位/标题位置的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在在显示我的方向相对于目标位置(而不是北部)一个谷歌地图视图我的位置显示一个箭头。

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)本人已使用myLocation.bearingTo(destLocation)中计算出的初始轴承从我的位置到目标位置;

b) I have calculated the initial bearing from my location to the destination location by using myLocation.bearingTo(destLocation);

我错过了最后一步;从这两个值(A和B)的公式是什么我用它来获得在手机指向相对于目标位置

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?

鸭preciate任何帮助的腐坏的头脑!

Appreciate any help for an addled mind!

推荐答案

好吧,我想通了这一点。对于任何人试图做到这一点,你需要:

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.

myLocation.bearingTo(destLocation);

C)偏角:真北和磁北之间的差异

c) declination: the difference between true north and magnetic north

从磁力仪+ accelermometer返回的标题为度以东真(磁)北(-180到+180),所以你需要得到北和磁北之间的差异为你的位置。这种差异是可变的取决于你在哪里在地球上。您可以通过使用GeomagneticField类获得。

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.

GeomagneticField geoField;

private final LocationListener locationListener = new LocationListener() {
   public void onLocationChanged(Location location) {
      geoField = new GeomagneticField(
         Double.valueOf(location.getLatitude()).floatValue(),
         Double.valueOf(location.getLongitude()).floatValue(),
         Double.valueOf(location.getAltitude()).floatValue(),
         System.currentTimeMillis()
      );
      ...
   }
}

有了这些你计算箭头的角度来绘制地图上的显示,你正面临着​​有关你的目标对象,而不是真正的北方。

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:

heading += geoField.getDeclination();

其次,你需要以抵消其手机从目标的目标,而不是真正的北向(航向)方向。这是我被困在零件上。从指南针返回的标题值给你描述(真北以度以东),其中,磁北极是一个值的关系,其中手机指向。因此,如如果值是-1​​0,你知道,磁北极是10度到你的左边。轴承向你的目标在度以东正北的角度。所以,你的赤纬已经补偿后,可以用下面的公式来获得期望的结果:

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:

heading = myBearing - (myBearing + heading); 

您会那么想从度转换向东真北(-180到+180)进入正常度(0〜360)的:

You'll then want to convert from degrees east of true north (-180 to +180) into normal degrees (0 to 360):

Math.round(-heading / 360 + 180)

这篇关于计算罗盘方位/标题位置的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆