【发布时间】:2013-05-01 05:44:26
【问题描述】:
我正在尝试为我正在从事的项目制作一个指南针应用程序,该应用程序可以在穿越崎岖地形时使用。我使用了标准的TYPE_ACCELEROMETER 和TYPE_MAGNETIC_FIELD 传感器,它似乎给出了相当准确的读数。但是,当我将手机围绕其 y 轴和 z 轴倾斜时,即使手机指向同一方向(恒定 z 轴),标题读数也会发生变化。
有谁知道如何弥补这一点?示例代码将不胜感激。
这是我目前正在使用的代码:
private void updateDirection() {
float[] R = new float[16];
float[] orientationValues = new float[3];
if(SensorManager.getRotationMatrix (R, null, accelerometerValues, magneticValues)){
SensorManager.getOrientation (R, orientationValues);
orientationValues[0] = (float)Math.toDegrees (orientationValues[0]);
orientationValues[1] = (float)Math.toDegrees (orientationValues[1]);
orientationValues[2] = (float)Math.toDegrees (orientationValues[2]);
if(orientationValues[0] < 0){
orientationValues[0] = 360 + orientationValues[0];
}
final int trueNorthHeading = NavigationUtils.getTrueNorthBearing(currentLocation, orientationValues[0]);
if(trueNorthHeading == currentHeading) {
return;
}
int accuracy = 3;
if(NavigationUtils.isSignificantHeadingChange(currentHeading, trueNorthHeading, accuracy, SIGNIFICANT_CHANGE_LIMIT)){
int headingChangeDegrees = NavigationUtils.getStearageDegree(currentHeading, trueNorthHeading);
currentHeading = trueNorthHeading;
navigationManager.headingUpdate(trueNorthHeading, Math.round(orientationValues[0]), headingChangeDegrees);
Log.d("COMPASS", "North: values[0]: " + (int)orientationValues[0]);
}
}
}
感谢您的帮助,
亚当
【问题讨论】:
-
您的意思是当您旋转它时,它被某种设备固定在适当位置时会发生轻微变化?还是比轻微的漂移更明显?
-
当围绕 y 轴旋转时,即使手机指向同一个方向,返回的值也会从 160* 变为 200*。 Defo 比轻微的漂移更重要。
-
手机是平放还是竖放?