如何使用android计算磁北偏移?

时间:2019-03-15 22:35:49

标签: android android-sensors compass-geolocation

我正在寻找一种计算向北偏移量的方法。 当设备处于横向或纵向模式时,我可以使其正常工作,但是在其他方向上,它将发送错误的读数。你做了类似的事情吗?目前,我正在使用来自加速度计和磁力计的数据,但我认为公式有问题。

public class Compass {

    private SensorManager mSensorManager;
    private Sensor accelerometer;
    private Sensor magnetometer;
    private CallbackContext callbackContext;
    private JSONObject data = new JSONObject();

    @Override
    public void initialize(CordovaInterface cordova, CordovaWebView webView) {
        super.initialize(cordova, webView);
        mSensorManager = (SensorManager) cordova.getActivity().getSystemService(Context.SENSOR_SERVICE);
        accelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        magnetometer = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);        
    }

    @Override
    public void onDestroy() {
        mSensorManager.unregisterListener(listener);
    }

    private SensorEventListener listener = new SensorEventListener() {

        float [] gravity;
        float [] geomagnetic;

        public void onSensorChanged(SensorEvent event) {

            if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
                gravity = event.values;
                try {
                    data.put("acc_x", event.values[0]);
                    data.put("acc_y", event.values[1]);
                    data.put("acc_z", event.values[2]);
                } catch(JSONException e) {}
            }

            if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
                geomagnetic = event.values;
                try {
                    data.put("mag_x", event.values[0]);
                    data.put("mag_y", event.values[1]);
                    data.put("mag_z", event.values[2]);
                } catch(JSONException e) {}
            }

            if (gravity != null && geomagnetic != null) {

                float[] rotationMatrix = new float[9];
                float[] inclinationMatrix = new float[9];
                float[] adjustedRotationMatrix = new float[9];

                if (SensorManager.getRotationMatrix(rotationMatrix, inclinationMatrix, gravity, geomagnetic)) {

                    float[] orientation = new float[3];
                    SensorManager.getOrientation(rotationMatrix, orientation);
                    float degree = (float) Math.toDegrees(orientation[0]);

                    SensorManager.remapCoordinateSystem(rotationMatrix, SensorManager.AXIS_X, SensorManager.AXIS_Z, adjustedRotationMatrix);
                    SensorManager.getOrientation(adjustedRotationMatrix, orientation);
                    float remapped = (float) Math.toDegrees(orientation[0]);

                    try { 
                        data.put("north_portrait", degree);
                        data.put("north_landscape", remapped);
                    } catch(JSONException e) {}

                }
             }
        }

        public void onAccuracyChanged(Sensor sensor, int accuracy) {}

    };

}

0 个答案:

没有答案