1

Android API デモにはコンパス アクティビティがありますが、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 デモをランドスケープ モードで正しく動作させる方法や、上記のコードの問題点がわかりません。

4

1 に答える 1

0

こちらをご覧ください: https://groups.google.com/forum/#!msg/android-beginners/V4pOfLn8klQ/mdL-Wh5A7tIJ

重力ベクトルに従って地磁気ベクトルを再マッピングする必要があります。つまり、加速度センサーと磁場センサーの両方をオンにしてイベントを送信する必要があります。

于 2012-03-27T05:37:21.910 に答える