【问题标题】:What is the correct way to read hardware compass?阅读硬件指南针的正确方法是什么?
【发布时间】:2012-03-27 13:37:21
【问题描述】:

在 Android API Demos 中有一个指南针活动,但它仅在我的 Google Nexus One 的纵向模式下显示正确的北方方向。

我在这里某处找到了以下代码:

private float[] magneticValues;
private float[] accelerometerValues;

@Override
public void onSensorChanged(SensorEvent event)
{
    switch (event.sensor.getType())
    {
        case Sensor.TYPE_MAGNETIC_FIELD:
            magneticValues = event.values.clone();
            break;
        case Sensor.TYPE_ACCELEROMETER:
            accelerometerValues = event.values.clone();
            break;
    }

    if (magneticValues != null && accelerometerValues != null)
    {
        float[] R = new float[16];
        SensorManager.getRotationMatrix(R, null, accelerometerValues, magneticValues);
        float[] orientation = new float[3];
        SensorManager.getOrientation(R, orientation);

        orientation[0] = (float) Math.toDegrees(orientation[0]);
        orientation[1] = (float) Math.toDegrees(orientation[1]);
        orientation[2] = (float) Math.toDegrees(orientation[2]);

        if (orientation[0] >= 360) orientation[0] -= 360;
        if (orientation[0] < 0) orientation[0] = 360 - orientation[0];
        if (orientation[1] >= 360) orientation[1] -= 360;
        if (orientation[1] < 0) orientation[1] = 360 - orientation[1];
        if (orientation[2] >= 360) orientation[2] -= 360;
        if (orientation[2] < 0) orientation[2] = 360 - orientation[2];

        azimuth = orientation[0];
        pitch = orientation[1];
        roll = orientation[2];

        updateView();
    }
}

但它没有任何意义。我不明白如何让 API 演示在横向模式下正常工作,或者上面的代码有什么问题。

【问题讨论】:

    标签: android android-sensors


    【解决方案1】:

    看看这里:https://groups.google.com/forum/#!msg/android-beginners/V4pOfLn8klQ/mdL-Wh5A7tIJ

    您需要根据重力矢量重新映射地磁矢量,即您需要同时开启加速度计和磁场传感器并发送事件

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-25
      相关资源
      最近更新 更多