title: How to rotate and move bus smoothly marker in google maps? - Android Studio
body: <p>I am trying to create a Uber like app. The bus marker is showing on map but I want to move the marker and rotate smoothly when the bus turn left or right on road. I called the rotateBusMarker(dl, false) function. The findDriverLocation() function retrieve the data from firebase and shows the bus marker on map. My code is given below.</p>
  <pre><code>private final HashMap&lt;String, Marker&gt; hm = new HashMap&lt;String, Marker&gt;();
  
      private void findDriverLocation() {
          DatabaseReference ref = FirebaseDatabase.getInstance().getReference(&quot;DrvUserLocation&quot;);
          ValueEventListener listener = new ValueEventListener() {
              @Override
              public void onDataChange(DataSnapshot dataSnapshot) {
                  for (DataSnapshot ds: dataSnapshot.getChildren()){
                      if (ds.exists()) {
                          String dNum = ds.child(&quot;drvMobN&quot;).getValue(String.class);
                          double lat = ds.child(&quot;Latitude&quot;).getValue(Double.class);
                          double lng = ds.child(&quot;Longitude&quot;).getValue(Double.class);
                          LatLng dl = new LatLng(lat, lng);
                          if(hm.containsKey(dNum)) {
                              hm.get(dNum).remove();
                          }
                          busMarker = mMap.addMarker(new MarkerOptions().position(dl).title(dNum).icon(BitmapDescriptorFactory.fromResource(R.drawable.bus_icon)));
                          hm.put(dNum, busMarker);
                          rotateBusMarker(dl, false);
                      } else {
                              Toast.makeText(StdMapsActivity.this, &quot;Latitude and Longitude Value Not Found!.&quot;, Toast.LENGTH_SHORT).show();
                      }
                  }
              }
  
              @Override
              public void onCancelled(DatabaseError databaseError) {
                  Toast.makeText(StdMapsActivity.this, &quot;Failed to get data.&quot;, Toast.LENGTH_SHORT).show();
              }
          };
          ref.addValueEventListener(listener);
      }
  
      public void rotateBusMarker(final LatLng toPosition, final boolean hideMarker) {
          final Handler handler = new Handler();
          final long start = SystemClock.uptimeMillis();
          Projection proj = mMap.getProjection();
          Point startPoint = proj.toScreenLocation(busMarker.getPosition());
          final LatLng startLatLng = proj.fromScreenLocation(startPoint);
          final Interpolator interpolator = new LinearInterpolator();
  
          handler.post(new Runnable() {
              @Override
              public void run() {
                  Location prevLoc = new Location(&quot;service Provider&quot;);
                  prevLoc.setLatitude(startLatLng.latitude);
                  prevLoc.setLongitude(startLatLng.longitude);
  
                  Location newLoc = new Location(&quot;service Provider&quot;);
                  newLoc.setLatitude(toPosition.latitude);
                  newLoc.setLongitude(toPosition.longitude);
  
                  float bearing = prevLoc.bearingTo(newLoc);
  
                  long elapsed = SystemClock.uptimeMillis() - start;
                  float t = interpolator.getInterpolation((float) elapsed / 1000);
                  double lng = t * toPosition.longitude + (1 - t) * startLatLng.longitude;
                  double lat = t * toPosition.latitude + (1 - t) * startLatLng.latitude;
                  busMarker.setPosition(new LatLng(lat, lng));
                  busMarker.setRotation(bearing);
  
                  if (t &lt; 1.0) {
                      // Post again 16ms later.
                      handler.postDelayed(this, 16);
                  } else {
                      if (hideMarker) {
                          busMarker.setVisible(false);
                      } else {
                          busMarker.setVisible(true);
                      }
                  }
              }
          });
      }
  </code></pre>