0

I am trying to animate a car marker along the street when I move. But the marker is drawn multiple times instead of moving on the street. I have tried multiple codes including this but the same result every time. Can someone point me what is wrong?

This is another one that isn't working for me.

public void animateMarker(GoogleMap myMap, final Marker marker, final ArrayList<LatLng> directionPoint) {
        final Handler handler = new Handler();
        final long start = SystemClock.uptimeMillis();
        Projection proj = myMap.getProjection();
        final long duration = 30000;

        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);
                if (i < directionPoint.size())
                    marker.setPosition(directionPoint.get(i));
                i++;

                if (t < 1.0) {
                    // Post again 16ms later.
                    handler.postDelayed(this, 16);
                } else {
                    if (marker.isVisible()) {
                        marker.remove();
                    } else {
                        marker.setVisible(true);
                    }
                }
            }
        });
    }
Community
  • 1
  • 1
The_Martian
  • 3,684
  • 5
  • 33
  • 61

2 Answers2

0

before calling
marker.setPosition(directionPoint.get(i));

remove the marker and then call it

Ragesh Ramesh
  • 3,470
  • 2
  • 14
  • 20
  • That solution throws npe on marker.setPosition line because marker is null. I did if (marker != null)marker.remove(); But I don't understand what you meant by "then call it". – The_Martian Feb 16 '16 at 05:47
0

before set marker clear the map and reset marker its work for you

if (mMap != null) {
    mMap.clear();
}
Dhaval Solanki
  • 4,589
  • 1
  • 23
  • 39