2011-03-29 25 views
10

我正在尝试实施使用方向传感器以指向特定位置的箭头。 Google地方信息为它找到的每个地方在ListView中实现此箭头。使用方向传感器指向特定位置

我已经设法得到方位角,但给定位置,我不知道如何继续计算我需要的角度。此外,我需要从真北和磁北转换。有没有人有这样的实施的例子?

在此先感谢。

回答

22

我解决了它。

float azimuth = // get azimuth from the orientation sensor (it's quite simple) 
Location currentLoc = // get location from GPS or network 
// convert radians to degrees 
azimuth = Math.toDegrees(azimuth); 
GeomagneticField geoField = new GeomagneticField(
      (float) currentLoc.getLatitude(), 
      (float) currentLoc.getLongitude(), 
      (float) currentLoc.getAltitude(), 
      System.currentTimeMillis()); 
azimuth += geoField.getDeclination(); // converts magnetic north to true north 
float bearing = currentLoc.bearingTo(target); // (it's already in degrees) 
float direction = azimuth - bearing; 

如果您要绘制箭头或其他指向方向的地方,请使用canvas.rotate(-direction)。由于画布旋转是逆时针的,因此我们传递负面的论点。

+3

您可以简化将方位角转换为度数:'azimuth = azimuth * 180 /(float)Math.PI;' – srgtuszy 2012-03-15 17:57:46

+4

使用Math.toDegrees(方位角) – Ge3ng 2013-06-17 22:18:57

+1

currentLoc.bearingTo(target);目标参数是什么? – mhdjazmati 2016-01-02 11:00:41