0

我无法解决titlename的问题。就像您在以下源代码中看到的那样,如果符号为负,我已经尝试在我的结果中添加 360。但它似乎没有很好地工作。

OnSensorChanged()

public void onSensorChanged(SensorEvent event) {

    if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
        mAccelerometer = event.values;
    }
    if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
        mGeomagnetic = event.values;
    }
    if (mAccelerometer != null && mGeomagnetic != null) {
        float R[] = new float[9];
        float I[] = new float[9];
        boolean success = SensorManager.getRotationMatrix(R, I,
                mAccelerometer, mGeomagnetic);
        if (success) {
            float orientation[] = new float[3];
            SensorManager.getOrientation(R, orientation);
            // An dieser Stelle enthält das Array orientation[] die Werte für azimuth, pitch und roll.


                double tempAzimuth = 180 * orientation[0] / Math.PI;

                if(tempAzimuth < 0){
                    swAzimuth = tempAzimuth + 360;
                }
                else{
                    swAzimuth = tempAzimuth;
                }
            //

            //double pitch = 180 * orientation[1] / Math.PI;
            //double roll = 180 * orientation[2] / Math.PI;



            txt.setText(String.valueOf(swAzimuth));

            Log.d("direction", String.valueOf(swAzimuth));
        }
    }


}
4

0 回答 0