所以,我已经花了 2 天时间修复我的 Android Compass 中的这个错误。 基本上我正在尝试使用 Xamarin.Android 构建 Qibla Compass。第一步应该是建立一个指向北方的指南针。
I have translated the JAVA code from this tutorial for my Compass to show proper North .
每当传感器读数发生变化时,我只是旋转包含我的罗盘图像的相对布局。
我曾尝试凭直觉调整代码,但这个简单的 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
我的设计代码 fragment :
<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 以获取加速度计读数,但我将其用于其他平滑目的,您可以像我一样将其从三轴剥离为一轴:
对于“真”北与磁北
您可以查看从您当前的偏角接收到的偏移量,并调整您从传感器获得的读数。
关于这个有很多问题/答案,这里只是一个:
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).
关于补偿手机倾斜和俯仰的有趣答案:
关于c# - Android Compass 不指向北方并不断变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37373848/