Android 指南针不指向北方并不断变化

Android Compass doesn't point North and keeps changing

所以,我已经花了 2 天时间修复我的 Android Compass 中的这个错误。 基本上我正在尝试使用 Xamarin.Android 构建一个 Qibla Compass。第一步应该是建立一个指向北方的指南针。

I have translated the JAVA code from this tutorial for my Compass to show proper North .

I am simply rotating my Relative Layout which contains my Compass Image whenever Sensor reading Changes.

我曾尝试凭直觉调整代码,但这个简单的 Compass 不喜欢呆在一个地方。我只是想让我的指南针图像的北方与地磁北方对齐。

然而我的指南针从来没有显示正确的北方而且北方一直在变化。

To check my device Compass indeed works I tested an App from Playstore and it ran pretty smooth.

我的Activity:

public class QiblaDirectionActivity : Activity, ISensorEventListener
//all the right inherits done

我的设计代码片段:

            <RelativeLayout
            android:layout_width="wrap_content"
            android:id="@+id/qiblaRL"
            android:layout_height="wrap_content">
            <ImageView
                android:id="@+id/qiblaCompass"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:scaleType="fitCenter"
                android:layout_centerInParent="true"
                android:src="@drawable/compass" />
            <ImageView
                android:id="@+id/qiblaArrow"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:scaleType="fitCenter"
                android:layout_centerInParent="true"
                android:src="@drawable/needle" />
        </RelativeLayout>

我的后端:

 float currentCompassDegree = 0f;
 public void OnSensorChanged(SensorEvent e)
    {
       //I have checked the accuracy its high
        RotateAnimation ra = new RotateAnimation(currentCompassDegree,-e.Values[0], Dimension.RelativeToSelf, 0.5f, Dimension.RelativeToSelf, 0.5f);
        ra.Duration = 120;
        ra.FillAfter = true;
        qiblaRL.StartAnimation(ra);

        currentCompassDegree = -e.Values[0];

    }

我在这里注册我的听众:

protected override void OnResume()
    {
        base.OnResume();
        if (_sensorManager != null)
        {

            var mF = _sensorManager.GetDefaultSensor(SensorType.MagneticField);
            _sensorManager.RegisterListener(this, mF, SensorDelay.Ui);
            qiblaAdvice.Text= "(Align Device Properly)";
        }
        else
        {
            qiblaAdvice.Text = "(Device doesn't support Compass)";
        }



    }

我在这里取消注册:

   protected override void OnPause()
    {
        base.OnPause();

        if (_sensorManager != null)
        {
            _sensorManager.UnregisterListener(this);
        }
    }

磁场传感器会包含很多噪声

您需要对其进行采样和平滑处理。

这是一个答案,其中有人将高通滤波器样本从 Apple/iOS 转换为 Android 以用于加速度计读数,但我将其用于其他平滑目的,您可以将其从三轴剥离'像我所做的那样:

"true" 北极与磁北极

您可以查看从您的当前赤纬接收到的偏移量,并调整您从传感器获得的读数。

这方面有很多question/answers,这里只是一个:

  • Calculate compass bearing / heading to location in Android

The declination of the horizontal component of the magnetic field from true north, in degrees (i.e. positive means the magnetic field is rotated east that much from true north).

关于补偿 phone 的倾斜和俯仰的有趣答案:

Android Compass that can Compensate for Tilt and Pitch