title: Rotating Marker in Google Maps API v2
body: <p>I have followed this answer and its working great. <a href="https://stackoverflow.com/a/37048987/4209417">https://stackoverflow.com/a/37048987/4209417</a></p>
  
  <p>But the issues I'm facing now are:</p>
  
  <ol>
  <li>When I stop at any location, its not stable. I'm getting random bearing value even when I'm not moving</li>
  <li>when I'm taking turns its rotating anticlockwise which is wrong. Its should take short turns.</li>
  </ol>
  
  <p>This is the code I'm using :</p>
  
  <pre><code>private double bearingBetweenLocations(LatLng latLng1,LatLng latLng2) {
  
          double PI = 3.14159;
          double lat1 = latLng1.latitude * PI / 180;
          double long1 = latLng1.longitude * PI / 180;
          double lat2 = latLng2.latitude * PI / 180;
          double long2 = latLng2.longitude * PI / 180;
  
          double dLon = (long2 - long1);
  
          double y = Math.sin(dLon) * Math.cos(lat2);
          double x = Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1)
                  * Math.cos(lat2) * Math.cos(dLon);
  
          double brng = Math.atan2(y, x);
  
          brng = Math.toDegrees(brng);
          brng = (brng + 360) % 360;
  
          return brng;
      }
  
  private void rotateMarker(final Marker marker, final float toRotation) {
          if(!isMarkerRotating) {
              final Handler handler = new Handler();
              final long start = SystemClock.uptimeMillis();
              final float startRotation = marker.getRotation();
              final long duration = 2000;
  
              final Interpolator interpolator = new LinearInterpolator();
  
              handler.post(new Runnable() {
                  @Override
                  public void run() {
                      isMarkerRotating = true;
  
                      long elapsed = SystemClock.uptimeMillis() - start;
                      float t = interpolator.getInterpolation((float) elapsed / duration);
  
                      float rot = t * toRotation + (1 - t) * startRotation;
  
                      float bearing =  -rot &gt; 180 ? rot / 2 : rot;
  
                      marker.setRotation(bearing);
  
                      CameraPosition camPos = CameraPosition
                              .builder(mMap.getCameraPosition())
                              .bearing(bearing)
                              .target(marker.getPosition())
                              .build();
                      mMap.animateCamera(CameraUpdateFactory.newCameraPosition(camPos));
  
                      if (t &lt; 1.0) {
                          // Post again 16ms later.
                          handler.postDelayed(this, 16);
                      } else {
                          isMarkerRotating = false;
                      }
                  }
              });
          }
      }
  </code></pre>
  
  <p>In <code>onLocationChanged()</code></p>
  
  <pre><code>float toRotation = (float) bearingBetweenLocations(toLatLng(oldLocation), toLatLng(newLocation));
  rotateMarker(my_marker, toRotation);
  </code></pre>