title: Android Google Maps auto-rotate
body: <p>I am trying to implement a map app with auto-rotate function (when pressing on corresponding button) based on compass heading. I used the answer of this question and adapt it to my project:
  <a href="https://stackoverflow.com/questions/14320015/android-maps-auto-rotate">android maps auto-rotate</a></p>
  
  <p>Since my device does not support Sensor.TYPE_ROTATION_VECTOR, I modified the code using Sensor.TYPE_ORIENTATION (deprecated). Currently, it works well. The only problem is that the rotation effect of the map has no continuity. The animation runs shakingly. I checked some other apps and I liked their rotation effect. Their map smoothly rotates.</p>
  
  <p>In order to eliminate this problem, can you advise an improvement? </p>
  
  <p>My implementation is this:</p>
  
  <pre><code>float mDeclination;
  private SensorManager mSensorManager;
  Sensor sensor;
  boolean isCompassOn = false;
  
  private GoogleMap map; // Might be null if Google Play services APK is not available.
  
  @Override
  public void onActivityCreated(Bundle savedInstanceState) {
      super.onActivityCreated(savedInstanceState);
      mSensorManager = (SensorManager) getActivity().getSystemService(Context.SENSOR_SERVICE);
      sensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
  
  }
  
  
  @Override
  public void onResume() {
      super.onResume();
      mSensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_UI);
  }
  
  @Override
  public void onPause() {
      mSensorManager.unregisterListener(this);
  }
  
  @Override
  public void onLocationChanged(Location location) {
  
      GeomagneticField field = new GeomagneticField(
              (float)location.getLatitude(),
              (float)location.getLongitude(),
              (float)location.getAltitude(),
              System.currentTimeMillis()
      );
  
      mDeclination = field.getDeclination();
  }
  
  @Override
  public void onSensorChanged(SensorEvent event) {
  
      if(event.sensor.getType() == Sensor.TYPE_ORIENTATION &amp;&amp; isCompassOn) {
          updateCamera(event.values[0] + mDeclination);
      }
  }
  
  private void updateCamera(float bearing) {
      CameraPosition oldPos = map.getCameraPosition();
      CameraPosition pos = CameraPosition.builder(oldPos).bearing(bearing).build();
      GoogleMap.CancelableCallback callback = new GoogleMap.CancelableCallback() {
          @Override
          public void onFinish() {
  
          }
  
          @Override
          public void onCancel() {
  
          }
      };
      map.animateCamera(CameraUpdateFactory.newCameraPosition(pos), 24, callback);
  }
  
  @Override
  public void onAccuracyChanged(Sensor sensor, int accuracy) {
  
  }
  </code></pre>