读取硬件指南针的正确方法是什么?

发布于 2024-12-09 20:45:18 字数 1611 浏览 1 评论 0原文

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 演示在横向模式下正确工作,或者上面的代码有什么问题。

There is a compass activity in Android API Demos but it shows correct direction to north only in portrait mode on my Google Nexus One.

I've found the following code somewhere here:

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();
    }
}

but it shows nothing sensible. I can not understand how to make API demo correctly work in landscape mode or what's wrong with the code above.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

樱花细雨 2024-12-16 20:45:18

请查看此处:https://groups.google。 com/forum/#!msg/android-beginners/V4pOfLn8klQ/mdL-Wh5A7tIJ

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

Have a look here: https://groups.google.com/forum/#!msg/android-beginners/V4pOfLn8klQ/mdL-Wh5A7tIJ

You need to remap the Geomagnetic vector according to the Gravity vector, ie you need both the Accelerometer and Magnetic Field sensors to be on and sending events

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文