title: How to move marker along polyline using google map
body: <p>I am trying to move the marker according to the polyline and with animation. Similar to the below image:</p>
  
  <p><a href="https://i.stack.imgur.com/z6l4U.png" rel="noreferrer"><img src="https://i.stack.imgur.com/z6l4U.png" alt="Taken from Mapbox"></a></p>
  
  <p><a href="https://www.mapbox.com/" rel="noreferrer">Mapbox</a> is already giving this kind of demo. But I want to achieve the same using Google maps. However right now my marker is not rotating along the path.
  Here is what I have tried:</p>
  
  <pre><code>private void onReady(List&lt;LatLng&gt; polyz) {
  
        for (int i = 0; i &lt; polyz.size() - 1; i++) {
          LatLng src = polyz.get(i);
          LatLng dest = polyz.get(i + 1);
          Polyline line = map.addPolyline(new PolylineOptions()
              .add(new LatLng(src.latitude, src.longitude),
                  new LatLng(dest.latitude, dest.longitude))
              .width(2).color(Color.RED).geodesic(true));
  
        }
        LatLngBounds.Builder builder = new LatLngBounds.Builder();
        builder.include(polyz.get(0));
        builder.include(polyz.get(polyz.size()-1));
        map.moveCamera(CameraUpdateFactory.newLatLngBounds(builder.build(), 48));
        map.animateCamera(CameraUpdateFactory.zoomTo(7), 1000, null);
        BitmapDescriptor icon = BitmapDescriptorFactory.fromResource(R.drawable.car);
        marker = map.addMarker(new MarkerOptions()
            .position(polyz.get(0))
            .title("Curr")
            .snippet("Move"));
        marker.setIcon(icon);
  
      }
  </code></pre>
  
  <p>And the animation:</p>
  
  <pre><code>    private void animateMarker(GoogleMap myMap, final Marker marker, final List&lt;LatLng&gt; directionPoint,
        final boolean hideMarker) {
      final Handler handler = new Handler();
      final long start = SystemClock.uptimeMillis();
      Projection proj = myMap.getProjection();
      final long duration = 600000;
  
      final Interpolator interpolator = new LinearInterpolator();
  
      handler.post(new Runnable() {
        int i = 0;
  
        @Override
        public void run() {
          long elapsed = SystemClock.uptimeMillis() - start;
          float t = interpolator.getInterpolation((float) elapsed
              / duration);
          Location location=new Location(String.valueOf(directionPoint.get(i)));
          Location newlocation=new Location(String.valueOf(directionPoint.get(i+1)));
          marker.setAnchor(0.5f, 0.5f);
          marker.setRotation(location.bearingTo(newlocation)  - 45);
          if (i &lt; directionPoint.size()) {
            marker.setPosition(directionPoint.get(i));
          }
          i++;
  
          if (t &lt; 1.0) {
            // Post again 16ms later.
            handler.postDelayed(this, 16);
          } else {
            if (hideMarker) {
              marker.setVisible(false);
            } else {
              marker.setVisible(true);
            }
          }
        }
      });
    }
  </code></pre>