How to get the camera direction of a GPS enabled phone?

All I found about GPS is that it simply indicates the location of the device, how do I get the direction that the camera is pointing, the angle and height of this device? How to make a compass (2D, 3D) using a GPS-enabled phone?

Is there a built-in 2D-3D compass in the phone or is it all done using GPS?

+4
source share
2 answers

I did it as follows:

1 - Get acceleration and magnetic values ​​using acceleration and magnetic lifts.

2 - Convert 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, the Values ​​accelerometer and magnetValues ​​float [3] the arrays that you initialize according to the listeners

3 - valuesValues ​​[0] and valuesValues ​​[1] - horizontal and vertical angles.

+5
source

The Maxims principle works, but the sensors are very noisy for most phones, and you will need to pass the sensor values ​​through some kind of filter (low pass or Kalman), or the numbers will “jump quickly” and be almost unusable.

outR will be in radians, so you will need to go to degrees:

 Math.toDegrees(values[0]); // for 0, 1, 2 (Azimuth, Pitch, Roll) 

The azimuth ( values[0] ) should be adjusted since it returns from 180 to -180, something like:

 if (values[0] < 0) values[0] = 360 + values[0]; // Translate from - to positive azimuth 
+3
source

All Articles