读取硬件指南针的正确方法是什么?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请查看此处: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