How to get directions on Android (e.g. North, West)

I am new to Android and I want to get directions according to my camera. How can I get direction information according to my camera? Could you give an idea for this?

+7
source share
4 answers

TYPE_ORIENTATION deprecated

We can no longer use the orientation sensor, we can use the magnetic field sensors and accelerometers in tandem to get equivalent functionality. This works more, but it allows you to continue to use the callback to handle orientation changes.

: http://www.codingforandroid.com/2011/01/using-orientation-sensors-simple.html

:

float[] mGravity;
float[] mGeomagnetic;

public void onSensorChanged(SensorEvent event) {

    if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
        mGravity = event.values;

    if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
        mGeomagnetic = event.values;

    if (mGravity != null && mGeomagnetic != null) {
        float R[] = new float[9];
        float I[] = new float[9];

        if (SensorManager.getRotationMatrix(R, I, mGravity, mGeomagnetic)) {

            // orientation contains azimut, pitch and roll
            float orientation[] = new float[3];
            SensorManager.getOrientation(R, orientation);

            azimut = orientation[0];
        }
    }
}

, :

float rotation = -azimut * 360 / (2 * 3.14159f);
+9

, , . :

TYPE_ORIENTATION .

+1

, , , 0 - 360, , ? LG G Pad Android 5.0.1

public void onSensorChanged(SensorEvent event) {
    if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
        mGravity = event.values;
    if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
        mGeomagnetic = event.values;
    if (mGravity != null && mGeomagnetic != null) {
        float R[] = new float[9];
        float outR[] = new float[9];
        float I[] = new float[9];

        boolean success = SensorManager.getRotationMatrix(R, I, mGravity, mGeomagnetic);
        if (success) {
            float orientation[] = new float[3];

            SensorManager.remapCoordinateSystem(R, SensorManager.AXIS_X, SensorManager.AXIS_Y, outR);

            SensorManager.getOrientation(outR, orientation);
            azimut = orientation[0];

            float degree = (float)(Math.toDegrees(azimut)+360)%360;

            System.out.println("degree " + degree);

, , , , , . , .., .

+1

, . SensorManager.getOrientation, 5 OpenGL. , - .

. , , , , , . , , (event.values[0]) 0. , . , .

enum Android , , Android, , .

enum class CompassCoordinateSystem { POCKET_COMPASS, REAR_CAMERA }

Then write SensorEventListenerto track changes. For clarity, I did not import android.hardware.SensorManager.*in the code below. All arrays defined below are filled with static SensorManager methods.

/** The latest compass orientation as a 3D vector. */
private var orientation3D = FloatArray(3)
private var coordinateSystem = CompassCoordinateSystem.REAR_CAMERA_ROTATION

fun compassDegrees(): Float = azimuthToDegrees(compassRadians())
fun compassRadians(): Float = orientation3D[0]

/** Convert such that North=0, East=90, South=180, West=270. */
fun azimuthToDegrees(azimuth: Float): Float {
    return ((Math.toDegrees(azimuth.toDouble())+360) % 360).toFloat()
}

override fun onSensorChanged(event: SensorEvent?) {
    if (event?.sensor?.type == Sensor.TYPE_ROTATION_VECTOR) {
         val rotationMatrix = FloatArray(9)
         SensorManager.getRotationMatrixFromVector(rotationMatrix, event.values)
         when (coordinateSystem) {
            CompassCoordinateSystem.POCKET_COMPASS -> SensorManager.getOrientation(rotationMatrix, orientation3D)
            CompassCoordinateSystem.REAR_CAMERA -> {
                val rearCameraMatrix = FloatArray(9)
                // The axis parameters for remapCoordinateSystem() are
                // from an example in that method documentation
                SensorManager.remapCoordinateSystem(rotationMatrix,
                    SensorManager.AXIS_X, 
                    SensorManager.AXIS_Z, 
                    rearCameraMatrix)
                SensorManager.getOrientation(rearCameraMatrix, orientation3D)
            }
        }
    }
}
-1
source

All Articles