【问题标题】:Gyroscope/Compass and Device Orientation陀螺仪/指南针和设备方向
【发布时间】:2012-12-12 09:08:42
【问题描述】:

我正在为 Android 制作一个指南针应用程序,并且我设法通过使用陀螺仪、加速度计和磁力计来捕获方位角(有关我使用的教程,请参阅 here)。不幸的是,只有当设备与地面平行放置(即平放在桌子上/我的手掌等)时,罗盘读数才起作用。每当我将设备倾斜到完全直立的位置(屏幕面向我)时,指南针都会给出错误的读数。我假设这是因为当设备的方向本身发生变化时轴会发生变化。我该如何防止这种情况发生?我希望指南针在这两个位置都可以工作:平行于地面和完全直立的位置。我该怎么做呢?如果有人可以推动我朝着正确的方向前进,那将非常有帮助。

感谢您的所有帮助! :D

【问题讨论】:

标签: android orientation accelerometer compass-geolocation gyroscope


【解决方案1】:

在您的 SensorFusionActivity 中添加 mDisplay 变量,如下所示:

private Display mDisplay = null;

然后,在 onCreate 方法中添加以下代码:

mDisplay = ((WindowManager)context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();

最后,这样修改calculateAccMagOrientation:

// calculates orientation angles from accelerometer and magnetometer output
public void calculateAccMagOrientation() {
    if(SensorManager.getRotationMatrix(rotationMatrix, null, accel, magnet)) {

        int rotation = mDisplay.getRotation();

        if(rotation == Surface.ROTATION_0)
            SensorManager.remapCoordinateSystem(rotationMatrix, SensorManager.AXIS_MINUS_Y, SensorManager.AXIS_X, rotationMatrix);
        else if(rotation == Surface.ROTATION_180)
            SensorManager.remapCoordinateSystem(rotationMatrix, SensorManager.AXIS_Y, SensorManager.AXIS_MINUS_X, rotationMatrix);
        else if(rotation == Surface.ROTATION_270)
            SensorManager.remapCoordinateSystem(rotationMatrix, SensorManager.AXIS_MINUS_X, SensorManager.AXIS_MINUS_Y, rotationMatrix);

        SensorManager.getOrientation(rotationMatrix, accMagOrientation);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多