无论屏幕方向如何,如何获得正确的方位(磁性方向)?

无论屏幕方向如何,如何获得正确的方位(磁性方向)?,第1张

无论屏幕方向如何,如何获得正确的方位(磁性方向)?

好的,我终于设法使代码生效了!

首先,我注册一个

Sensor.TYPE_MAGNETIC_FIELD
and
Sensor.TYPE_GRAVITY
:(就像阮阮说的那样!)

private void initSensors() {    LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);    SensorManager sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);    Sensor mSensorGravity = sensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY);    Sensor mSensorMagneticField = sensorManager .getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);        if (mSensorGravity != null) {        Log.i(TAG, "Gravity sensor available. (TYPE_GRAVITY)");        sensorManager.registerListener(mSensorEventListener,     mSensorGravity, SensorManager.SENSOR_DELAY_GAME);    } else {        Log.i(TAG, "Gravity sensor unavailable. (TYPE_GRAVITY)");    }        if (mSensorMagneticField != null) {        Log.i(TAG, "Magnetic field sensor available. (TYPE_MAGNETIC_FIELD)");        sensorManager.registerListener(mSensorEventListener,     mSensorMagneticField, SensorManager.SENSOR_DELAY_GAME);    } else {        Log.i(TAG,     "Magnetic field sensor unavailable. (TYPE_MAGNETIC_FIELD)");    }}

我将其

SensorEventListner
用于计算:

private SensorEventListener mSensorEventListener = new SensorEventListener() {    @Override    public void onAccuracyChanged(Sensor sensor, int accuracy) {    }    @Override    public void onSensorChanged(SensorEvent event) {        if (event.sensor.getType() == Sensor.TYPE_GRAVITY) { mGravity = event.values.clone();        } else if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) { mMagnetic = event.values.clone();        }        if (mGravity != null && mMagnetic != null) {  float[] rotationMatrix = new float[9]; if (SensorManager.getRotationMatrix(rotationMatrix, null,         mGravity, mMagnetic)) {          // http://android-developers.blogspot.de/2010/09/one-screen-turn-deserves-another.html     float[] remappedRotationMatrix = new float[9];     switch (getWindowManager().getDefaultDisplay()  .getRotation()) {     case Surface.ROTATION_0:         SensorManager.remapCoordinateSystem(rotationMatrix,      SensorManager.AXIS_X, SensorManager.AXIS_Y,      remappedRotationMatrix);         break;     case Surface.ROTATION_90:         SensorManager.remapCoordinateSystem(rotationMatrix,      SensorManager.AXIS_Y,      SensorManager.AXIS_MINUS_X,      remappedRotationMatrix);         break;     case Surface.ROTATION_180:         SensorManager.remapCoordinateSystem(rotationMatrix,      SensorManager.AXIS_MINUS_X,      SensorManager.AXIS_MINUS_Y,      remappedRotationMatrix);         break;     case Surface.ROTATION_270:         SensorManager.remapCoordinateSystem(rotationMatrix,      SensorManager.AXIS_MINUS_Y,      SensorManager.AXIS_X, remappedRotationMatrix);         break;     }          float results[] = new float[3];     SensorManager.getOrientation(remappedRotationMatrix,  results);          float current_measured_bearing = (float) (results[0] * 180 / Math.PI);     if (current_measured_bearing < 0) {         current_measured_bearing += 360;     }          current_measured_bearing = current_measured_bearing  + SMOOTHING_FACTOR_COMPASS  * (current_measured_bearing - compass_last_measured_bearing);          visual_compass_value.setText(String.valueOf(Math  .round(current_bearing))  + getString(R.string.degrees));          compass_last_measured_bearing = current_measured_bearing; }        }    }};


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5131617.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-17
下一篇 2022-11-17

发表评论

登录后才能评论

评论列表(0条)

保存