titlenameの問題が解けません。次のソースコードでわかるように、符号が負の場合、結果に 360 を追加しようとしました。しかし、うまくいかなかったようです。
OnSensorChanged()
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
mAccelerometer = event.values;
}
if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
mGeomagnetic = event.values;
}
if (mAccelerometer != null && mGeomagnetic != null) {
float R[] = new float[9];
float I[] = new float[9];
boolean success = SensorManager.getRotationMatrix(R, I,
mAccelerometer, mGeomagnetic);
if (success) {
float orientation[] = new float[3];
SensorManager.getOrientation(R, orientation);
// An dieser Stelle enthält das Array orientation[] die Werte für azimuth, pitch und roll.
double tempAzimuth = 180 * orientation[0] / Math.PI;
if(tempAzimuth < 0){
swAzimuth = tempAzimuth + 360;
}
else{
swAzimuth = tempAzimuth;
}
//
//double pitch = 180 * orientation[1] / Math.PI;
//double roll = 180 * orientation[2] / Math.PI;
txt.setText(String.valueOf(swAzimuth));
Log.d("direction", String.valueOf(swAzimuth));
}
}
}