【问题标题】:Android Compass BearingAndroid 指南针方位
【发布时间】:2013-03-01 10:53:09
【问题描述】:

我正在尝试使用以下方法以度数(即 0-360)获取指南针方位:

float[] mGravity;
float[] mGeomagnetic;
public void onSensorChanged(SensorEvent event) {
    if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
        mGravity = event.values;
    if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
        mGeomagnetic = event.values;
    if (mGravity != null && mGeomagnetic != null) {
        float R[] = new float[9];
        float I[] = new float[9];
        boolean success = SensorManager.getRotationMatrix(R, I, mGravity,
                mGeomagnetic);
        if (success) {
            float orientation[] = new float[3];
            SensorManager.getOrientation(R, orientation);
            float azimut = orientation[0];
            bearing.setText("Bearing: "+ azimut);
        }
    }
}

方位角值(即orientation[0])应该是 0

【问题讨论】:

    标签: android magnetometer sensormanager


    【解决方案1】:

    数值以弧度为单位,您必须转换为弧度

    int azimut = (int) Math.round(Math.toDegrees(orientation[0]));
    

    【讨论】:

    • 非常感谢@Hoan Nguyen
    • orientation[0] 似乎只提供了自上次阅读以来的差异。我已将值转换为度数,但值仍然很小。有没有办法在一次读数中获得设备的实际方位(0 - 360 度)?
    • 这不是差异,而是相对于磁北的实际方位。如果您的读数总是很小,那么您的代码就有问题。
    • 原来我的指南针没有校准,在执行完数字 8 运动后(由我下载的另一个指南针应用程序提示,它也显示了不正确的值),我现在收到的值是我所期望的.
    【解决方案2】:

    确实是用弧度表示的。谢谢霍恩。我添加了一些逻辑来获得从 0 到 360 度的方位,因为如果我只将它转换为度,我得到的值是从 -180 到 180。

    float azimuthInRadians = orientation[0];
    float azimuthInDegress = (float)Math.toDegrees(azimuthInRadians)+360)%360;
    

    【讨论】:

      【解决方案3】:
      // This answer applies to Google Maps api v2.
      // It is possible by registering your application with Sensor Listener for Orientation and get the
      // angle relative to true north inside onSensorChanged and update camera accordingly.
      // Angle can be used for bearing. Following code can be used:
      
      // Instead of using Sensor.TYPE_ORIENTATION try using getOrinetation api. Sensor.TYPE_ORIENTATION
      // has been deprecated.
      
      @Override
      protected void onResume() {
          // TODO Auto-generated method stub
          super.onResume();
          if (sensorManager != null)
              sensorManager.registerListener(this,
                      sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
                      SensorManager.SENSOR_DELAY_GAME);
      }
      
      public void onSensorChanged(SensorEvent event) {
      
          float compassBearingRelativeToTrueNorth = Math.round(event.values[0]);
      
          Log.d(TAG, "Degree ---------- " + degree);
      
          updateCamera(compassBearingRelativeToTrueNorth);
      
      }
      
      private void updateCamera(float bearing) {
          CameraPosition oldPos = googleMap.getCameraPosition();
      
          CameraPosition pos = CameraPosition.builder(oldPos).bearing(bearing)
                  .build();
      
          googleMap.moveCamera(CameraUpdateFactory.newCameraPosition(pos));
      
      }
      

      【讨论】:

        猜你喜欢
        • 2011-05-17
        • 2013-10-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-06-02
        相关资源
        最近更新 更多