All i found about GPS is that it just tells the location of the device, how do one get the direction where camera is pointing, its angle, and height of that device? How to make compass (2D, 3D) with GPS enabled phone?
Is there a built in 2D, 3D compass in phone or it is all done by just GPS?
I did it this way:
1 - Get acceleration and magnetic values using acceleration and magnetic lesteners.
2 - Transform these values to orientation:
private float[] rMatrix = new float[9];
private float[] outR = new float[9];
SensorManager.getRotationMatrix(rMatrix, null, accelerometerValues, magneticValues);
SensorManager.remapCoordinateSystem(rMatrix, SensorManager.AXIS_X, SensorManager.AXIS_Z, outR);
SensorManager.getOrientation(outR, orientationValues);
Here accelerometerValues and magneticValues float[3] arrays that you initialize in according listeners
3 - orientationValues[0] and orientationValues[1] values are horizontal and vertical angles.
Maxims method works but the sensors are very noisy on most phones and you will need to pass the values of the sensor through some type of filter (low pass or kahlman) or the numbers will "jump" around rapidly and be almost unusable.
outR
will be in radians so you will need to change to degrees:
Math.toDegrees(values[0]); // for 0, 1, 2 (Azimuth, Pitch, Roll)
The azimuth (values[0]
) has to be adjusted since it returns 180 through -180 so something like:
if (values[0] < 0) values[0] = 360 + values[0]; // Translate from - to positive azimuth
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With