How to get Direction in Android (Such as North, West)

前端 未结 4 998
伪装坚强ぢ
伪装坚强ぢ 2020-12-10 16:09

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

4条回答
  •  无人及你
    2020-12-10 16:26

    Here's what I have so far that's somewhat working for me, values returned are between 0 - 360 but I don't think north is properly calibrated? I'm using this on an LG G Pad running 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);
    

    I'm sure there are things I've missed but hopefully this is a decent starting point for others. I reviewed a good number of other questions, comments, etc. to get to this point.

提交回复
热议问题