1

I am using this code and get reference from stack overflow for moving marker from one position to another by showing moving marker with animation

MapActivity.java

double[] startValues = new double[]{marker.getPosition().latitude, marker.getPosition().longitude};
double[] endValues = new double[]{destLatLng.latitude, destLatLng.longitude};
ValueAnimator latLngAnimator = ValueAnimator.ofObject(new DoubleArrayEvaluator(), startValues, endValues);
latLngAnimator.setDuration(600);
latLngAnimator.setInterpolator(new DecelerateInterpolator());
latLngAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
    @Override
    public void onAnimationUpdate(ValueAnimator animation) {
        double[] animatedValue = (double[]) animation.getAnimatedValue();
        marker.setPosition(new LatLng(animatedValue[0], animatedValue[1]));
    }
});
latLngAnimator.start();

DoubleArrayEvaluator .java

import android.animation.TypeEvaluator;

/**
 * Inspired from {@link android.animation.FloatArrayEvaluator}
 * <p/>
 * This evaluator can be used to perform type interpolation between <code>double[]</code> values.
 * Each index into the array is treated as a separate value to interpolate. For example,
 * evaluating <code>{100, 200}</code> and <code>{300, 400}</code> will interpolate the value at
 * the first index between 100 and 300 and the value at the second index value between 200 and 400.
 */
public class DoubleArrayEvaluator implements TypeEvaluator<double[]> {

    private double[] mArray;

    /**
     * Create a DoubleArrayEvaluator that does not reuse the animated value. Care must be taken
     * when using this option because on every evaluation a new <code>double[]</code> will be
     * allocated.
     *
     * @see #DoubleArrayEvaluator(double[])
     */
    public DoubleArrayEvaluator() {
    }

    /**
     * Create a DoubleArrayEvaluator that reuses <code>reuseArray</code> for every evaluate() call.
     * Caution must be taken to ensure that the value returned from
     * {@link android.animation.ValueAnimator#getAnimatedValue()} is not cached, modified, or
     * used across threads. The value will be modified on each <code>evaluate()</code> call.
     *
     * @param reuseArray The array to modify and return from <code>evaluate</code>.
     */
    public DoubleArrayEvaluator(double[] reuseArray) {
        mArray = reuseArray;
    }

    /**
     * Interpolates the value at each index by the fraction. If
     * {@link #DoubleArrayEvaluator(double[])} was used to construct this object,
     * <code>reuseArray</code> will be returned, otherwise a new <code>double[]</code>
     * will be returned.
     *
     * @param fraction   The fraction from the starting to the ending values
     * @param startValue The start value.
     * @param endValue   The end value.
     * @return A <code>double[]</code> where each element is an interpolation between
     * the same index in startValue and endValue.
     */
    @Override
    public double[] evaluate(float fraction, double[] startValue, double[] endValue) {
        double[] array = mArray;
        if (array == null) {
            array = new double[startValue.length];
        }

        for (int i = 0; i < array.length; i++) {
            double start = startValue[i];
            double end = endValue[i];
            array[i] = start + (fraction * (end - start));
        }
        return array;
    }
}

But Marker is not moving from one position to another it just put a new marker on new position. i want that it will show marker moving from one point to another like uber.

Rover
  • 661
  • 2
  • 18
  • 39
  • You can see the answer here https://stackoverflow.com/questions/13728041/move-markers-in-google-map-v2-android – Moklesur Rahman Oct 01 '19 at 11:54
  • @MMR i tried everything from this page but marker not moving with animation it just added to new location – Rover Oct 01 '19 at 12:02

1 Answers1

0

This can be a Hacky solution, you remove the marker from the existing position and then add it to the new location :

if(marker!=null)
marker.remove();
marker = mMap.addMarker(new MarkerOptions().position(lat_lng).title("You"));
// mMap.animateCamera( CameraUpdateFactory.zoomTo( 21.0f ) );
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(lat_lng, 16));

Hope this helps.

Vikas
  • 136
  • 6
  • I can add this. but the thing is i wanna show moving on map like cars moving from one place to another via road. i want to show marker like that move via a road path. – Rover Oct 01 '19 at 12:27
  • Have you tried this : https://stackoverflow.com/questions/35332968/update-marker-position-on-google-map-smoothly?rq=1 – Vikas Oct 01 '19 at 12:33
  • Here is another solution I have got for you to try : https://codinginfinite.com/android-example-animate-marker-map-current-location/ – Vikas Oct 01 '19 at 12:37