2011-03-29 98 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