2017-01-23 66 views
0

我正在使用SensorEvent API爲了從我的應用程序從不同的傳感器(更具體地說:TYPE_ROTATION_VECTOR,TYPE_GRAVITY,TYPE_GYROSCOPE,TYPE_LINEAR_ACCELERATION)獲取數據。現在,我知道在iOS中有所謂的CMAttitudeReferenceFrameXTrueNorthZVertical,它給出所有的傳感器值與True North相關,而z軸始終是垂直的。傳感器數據相對於真正的北android

我在Android中找不到任何類似的東西,所以我在考慮手動翻譯座標系。我也在考慮使用remapCoordinateSystem method。但是,我仍然不知道如何獲得關於True North的數據。有沒有人需要處理類似的事情?

+0

我想您的問題可能已被回答[這裏](http://stackoverflow.com/a/5518318/7071399)乾杯 –

+0

嗨,我知道這個職位。但它並不能幫助我,因爲我需要的是一個新的座標系,不僅僅是這個帖子中的一個值。 – pixie

回答

0

此答案由使用here類啓發:

YOUT應該有onSensorChanged方法在SensorEventListener

@Override 
     public void onSensorChanged(SensorEvent event) { 
      if (event.sensor.getType() == Sensor.TYPE_ROTATION_VECTOR) { 
       //get the rotation matrix from the sensor (this will be using the magnetic north) 
       SensorManager.getRotationMatrixFromVector(mRotationMatrix, event.values); 
       //change the coordinate system (your new matrix is in the variable mChangedRotationMatrix) 
       SensorManager.remapCoordinateSystem(mRotationMatrix, SensorManager.AXIS_Y, 
         SensorManager.AXIS_MINUS_X, mChangedRotationMatrix); 
       //get the orientationMatrix of your new coordinate system 
       SensorManager.getOrientation(mChangedRotationMatrix, mOrientation); 
       //get the magnetic heading 
       float magneticHeading = (float) Math.toDegrees(mOrientation[0]); 
       //adjust accordingly by calculating the true north with either the "computeTrueNorth" method available the above link or the method used in the link below 

      } 
     } 

要獲得學位正北你可以用this answer