私が取り組んでいるプロジェクトのコンパス アプリを作ろうとしています。標準TYPE_ACCELEROMETER
とTYPE_MAGNETIC_FIELD
センサーを使用しましたが、かなり正確な測定値が得られるようです。ただし、電話を y 軸と z 軸の周りに傾けると、電話が同じ方向 (一定の z 軸) を指していても、Heading の読み取り値が変わります。
これを補う方法を知っている人はいますか?サンプルコードをいただければ幸いです。
現在使用しているコードは次のとおりです。
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]);
}
}
}
ご協力いただきありがとうございます、
アダム