title: Android rotate map based on user heading and bearing while navigation
body: <p>I want to rotate map while user navigation based on his direction/heading and and his current segment(current location to next).  I have route drawn on map and i am calculating bearing between two points using method <code>current.bearingTo(destination)</code>. I have followed <a href="https://stackoverflow.com/questions/7978618/rotating-an-imageview-like-a-compass-with-the-north-pole-set-elsewhere">this</a> link but results are not as i was expecting. What i want to achieve is that when user starts route navigation app should take current segment's (start and end point) and calculate bearing between two points, also get heading/direction from sensor and rotate the map. Then on segment completion when user takes turn (left or right) now again calculate new bearing(of next segment's start/end point) and heading direction and rotate map accordingly. Here is my onSensorChanged method;</p>
  
  <pre><code>@Override
  public void onSensorChanged( SensorEvent event ) {
  
      Location LocationObj = LocationService.mLocation;
      // If we don't have a Location, we break out
      if ( LocationObj == null ) return;
  
      float azimuth = event.values[0];
      float baseAzimuth = azimuth;
  
      GeomagneticField geoField = new GeomagneticField( Double
              .valueOf( LocationObj.getLatitude() ).floatValue(), Double
              .valueOf( LocationObj.getLongitude() ).floatValue(),
              Double.valueOf( LocationObj.getAltitude() ).floatValue(),
              System.currentTimeMillis() );
  
      azimuth -= geoField.getDeclination(); // converts magnetic north into true north
  
      Location destinationObj = new Location("");
      destinationObj.setLongitude(33.521398);
      destinationObj.setLatitude(73.090826);
      // Store the bearingTo in the bearTo variable
      float bearTo = LocationObj.bearingTo( destinationObj );
  
      // If the bearTo is smaller than 0, add 360 to get the rotation clockwise.
      if (bearTo &lt; 0) {
          bearTo = bearTo + 360;
      }
  
      //This is where we choose to point it
      float direction = bearTo - azimuth;
  
      // If the direction is smaller than 0, add 360 to get the rotation clockwise.
      if (direction &lt; 0) {
          direction = direction + 360;
      }
  
      //Log.i(TAG, "rotation sensor angle : " + direction);
      //mapController.setMapRotation((float) Math.toRadians(direction));
      //rotateImageView( arrow, R.drawable.arrow, direction );
  
      //Set the field
      String bearingText = "N";
  
      if ( (360 &gt;= baseAzimuth &amp;&amp; baseAzimuth &gt;= 337.5) || (0 &lt;= baseAzimuth &amp;&amp; baseAzimuth &lt;= 22.5) ) bearingText = "N";
      else if (baseAzimuth &gt; 22.5 &amp;&amp; baseAzimuth &lt; 67.5) bearingText = "NE";
      else if (baseAzimuth &gt;= 67.5 &amp;&amp; baseAzimuth &lt;= 112.5) bearingText = "E";
      else if (baseAzimuth &gt; 112.5 &amp;&amp; baseAzimuth &lt; 157.5) bearingText = "SE";
      else if (baseAzimuth &gt;= 157.5 &amp;&amp; baseAzimuth &lt;= 202.5) bearingText = "S";
      else if (baseAzimuth &gt; 202.5 &amp;&amp; baseAzimuth &lt; 247.5) bearingText = "SW";
      else if (baseAzimuth &gt;= 247.5 &amp;&amp; baseAzimuth &lt;= 292.5) bearingText = "W";
      else if (baseAzimuth &gt; 292.5 &amp;&amp; baseAzimuth &lt; 337.5) bearingText = "NW";
      else bearingText = "?";
  
      //fieldBearing.setText(bearingText);
      Log.i(TAG, "rotation sensor azimuth : " + bearingText);
  
  }
  </code></pre>