方位回到0

时间:2015-09-15 16:13:20

标签: android android-sensors azimuth

我正试图在拍照时获得用户手机的方位角。但是,当我将方位角放在屏幕上仅用于当前的调试目的时,它总是回到0.0。我是使用传感器的新手,所以我想弄明白这一点。我的代码是

private void dispatchTakePictureIntent() {
    double latitude, longitude;
    if (mGpsLocationTracker.canGetLocation()) {
        latitude = mGpsLocationTracker.getLatitude();
        longitude = mGpsLocationTracker.getLongitude();
    } else {
        latitude = 0;
        longitude = 0;
    }
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
    }

    Toast.makeText(this, "" + latitude + ", " + longitude, Toast.LENGTH_SHORT).show();

    getDirection ();

    Toast.makeText(this, Float.toString(degree), Toast.LENGTH_SHORT).show();
}

public void getDirection() {
    SensorManager mySensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
    List<Sensor> mySensors = mySensorManager.getSensorList(Sensor.TYPE_ORIENTATION);
    if(mySensors.size() > 0){
        mySensorManager.registerListener(mySensorEventListener, mySensors.get(0), SensorManager.SENSOR_DELAY_UI);
    }
}

/*
DOES NOT WORK. NEED TO FIX
 */
private SensorEventListener mySensorEventListener = new SensorEventListener(){

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        // TODO Auto-generated method stub
        float radianValue = Math.round(event.values[0]);
        degree = (float) Math.toDegrees(radianValue);
        if (degree < 0.0f) {
            degree += 360.0f;
        }

    }
};

public void takePicture (View view) {
    dispatchTakePictureIntent();
}

任何人都可以帮助我解决我的错误

2 个答案:

答案 0 :(得分:0)

方向传感器不是以度为单位返回角度吗?你假设弧度并转换为度数。鉴于此,它仍然不应该给你0。

答案 1 :(得分:0)

TYPE_ORIENTATION已折旧且未提供正确的结果 如果设备不平坦。您需要使用TYPE_ACCELEROMETERTYPE_MAGNETIC_FIELD获取rotation matrix,然后在致电remapCoordinateSystem之前致电getOrientation

相关问题