我正在制作程序,用于在相机上显示来自地图的对象,这几乎可以很好地工作,除了从垂直方向左右几度(如80-110和260-280度)。在其他+ -320度,它运作良好。我已经尝试使用TYPE_ROTATION_VECTOR和带磁力计的加速度计,它们具有相同的结果。有人知道任何解决方案吗?
使用TYPE_ROTATION_VECTOR:
if (event.sensor.getType() == Sensor.TYPE_ROTATION_VECTOR)
{
float[] roationV = new float[16];
SensorManager.getRotationMatrixFromVector(roationV, event.values);
float[] orientationValuesV = new float[3];
SensorManager.getOrientation(roationV, orientationValuesV);
tvHeading.setText(String.format(
"Coordinates: lat = %1$.2f, lon = %2$.2f, time = %3$.2f",
orientationValuesV[0], orientationValuesV[1], orientationValuesV[2]));
float[] rotationMatrix=new float[16];
mSensorManager.getRotationMatrixFromVector(rotationMatrix, event.values);
float[] orientationValues = new float[3];
SensorManager.getOrientation(rotationMatrix, orientationValues);
double azimuth = Math.toDegrees(orientationValues[0]);
double pitch = Math.toDegrees(orientationValues[1]);
double roll = Math.toDegrees(orientationValues[2]);
tvOrientation.setText(String.format(
"Coordinates: lat = %1$.2f, lon = %2$.2f, time = %3$.2f",
azimuth,pitch,roll));
}
带加速度计+磁力计
if (event.sensor == mAccelerometer) {
System.arraycopy(event.values, 0, mLastAccelerometer, 0, event.values.length);
mLastAccelerometer = meanFilterAccelSmoothing
.addSamples(mLastAccelerometer);
mLastAccelerometer = medianFilterAccelSmoothing
.addSamples(mLastAccelerometer);
for (int i = 0; i < mLastAccelerometer.length; i++) {
mLastAccelerometer[i] = (float) Math.floor(mLastAccelerometer[i] * 1000) / 1000;
}
mLastAccelerometerSet = true;
}
if (event.sensor == mMagnetometer) {
System.arraycopy(event.values, 0, mLastMagnetometer, 0, event.values.length);
mLastMagnetometer = meanFilterMagneticSmoothing.addSamples(mLastMagnetometer);
mLastMagnetometer = medianFilterMagneticSmoothing.addSamples(mLastMagnetometer);
for (int i = 0; i < mLastMagnetometer.length; i++) {
mLastMagnetometer[i] = (float) Math.floor(mLastMagnetometer[i] * 1000) / 1000;
}
mLastMagnetometerSet = true;
}
if (mLastAccelerometerSet && mLastMagnetometerSet) {
SensorManager.getRotationMatrix(mR, null, mLastAccelerometer, mLastMagnetometer);
SensorManager.getOrientation(mR, mOrientation);
if (angeles.size() > 0) {
for (int i = 0; i < mapObjects.size(); i++) {
compassFunc(i, mOrientation[0], mOrientation[1], mOrientation[2]);
}
}
private void compassFunc(int number, float... values) {
double angularXSpeed = Math.floor(values[0] * 180 / Math.PI * 100) / 100;
double angularYSpeed = Math.floor(values[1] * 180 / Math.PI * 100) / 100;
double angularZSpeed = Math.floor(values[2] * 180 / Math.PI * 100) / 100;
tvOrientation.setText(String.format(
"Screen: lt= %1$.2f : %2$.2f,rt= %3$.2f : %4$.2f,lb= %5$.2f : %6$.2f,rb= %7$.2f : %8$.2f",
xLeftTop, yLeftTop, xRightTop,yRightTop,xLeftBottom,yLeftBottom,xRightBottom,yRightBottom));
}