使用方向传感器指向特定位置

Flá*_*ria 10 android location sensor orientation compass-geolocation

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

我已经设法获得了方位角,但是给定了一个位置,我不知道如何继续计算我需要的角度.而且,我需要从真正的北方和磁北方进行转换.有没有人有这种实施的例子?

提前致谢.

Flá*_*ria 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;
Run Code Online (Sandbox Code Playgroud)

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

  • 使用Math.toDegrees(方位角)更容易 (4认同)
  • 您可以简化将方位角转换为度数:`azimuth = azimuth*180 /(float)Math.PI;` (3认同)