onsensorchanged谷歌Map旋转

t9eec4r0  于 2021-07-13  发布在  Java
关注(0)|答案(0)|浏览(254)

有点问题 onSensorChange ,我可以把我的Map从朝北移动,我的代码是正确的,但它总是旋转,即使手机不动

@Override
    protected void onResume() {
        mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
        mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        mMagnetometer = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
        super.onResume();
        mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_STATUS_ACCURACY_LOW);
        mSensorManager.registerListener(this, mMagnetometer, SensorManager.SENSOR_STATUS_ACCURACY_LOW);
    }
    @Override
    public void onSensorChanged(SensorEvent event) {
        if (event.sensor == mAccelerometer) {
            System.arraycopy(event.values, 0, mLastAccelerometer, 0, event.values.length);
            mLastAccelerometerSet = true;
        } else if (event.sensor == mMagnetometer) {
            System.arraycopy(event.values, 0, mLastMagnetometer, 0, event.values.length);
            mLastMagnetometerSet = true;
        }
        if (mLastAccelerometerSet && mLastMagnetometerSet) {
            final Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                SensorManager.getRotationMatrix(mR, null, mLastAccelerometer, mLastMagnetometer);
                SensorManager.getOrientation(mR, mOrientation);
                float azimuthInRadians = mOrientation[0];
                float azimuthInDegress = (float)(Math.toDegrees(azimuthInRadians)+360)%360;
                RotateAnimation ra = new RotateAnimation(
                mCurrentDegree,
                -azimuthInDegress,
                Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF,
                0.5f);

                updateCamera(azimuthInDegress);
                }
            }, 3000);

        }

    }

    private void updateCamera(final Float bearing) {

        CameraPosition pos = CameraPosition.builder().target(latLng).zoom(20).bearing(bearing).build();
        mGoogleMap.moveCamera(CameraUpdateFactory.newCameraPosition(pos));

    }

我已经把延迟5秒的时间,但它不工作,请帮助我谢谢

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题