0

Hello I am trying to update the marker from one position to another in google maps. I want to change the location of the marker with animation just like uber & Ola . Sample code below. I am not getting any error. I am just not seeing the animation. Any simple animation will do.

Code

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;
    private static int SPLASH_TIME_OUT = 5000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }



    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        // Add a marker in tilak nagar
        LatLng tilkanagar = new LatLng(28.6353, 77.0971);

        LatLng Firstmarker = new LatLng(28.6350, 77.0970);
        LatLng FirstmarkerTo = new LatLng(28.6356, 77.0972);

        LatLng Secondmarker = new  LatLng(28.6345, 77.0969);
        LatLng SecondmarkerTo = new LatLng(28.6347, 77.0967);


        mMap.addMarker(new MarkerOptions().position(tilkanagar).title("Hello I am at Techvision"));
        Marker marker = mMap.addMarker(new MarkerOptions().position(Firstmarker).title("1 Minute Away").icon(BitmapDescriptorFactory.fromResource(R.drawable.car)));
        Marker marker1 = mMap.addMarker(new MarkerOptions().position(Secondmarker).title("2 Minutes Away").icon(BitmapDescriptorFactory.fromResource(R.drawable.car)));


        mMap.moveCamera(CameraUpdateFactory.newLatLng(tilkanagar));
        mMap.animateCamera(CameraUpdateFactory.zoomTo(18.0f));


                    animateMarker(marker, FirstmarkerTo, false);
                    animateMarker(marker1,SecondmarkerTo,false);

    }


    public void animateMarker(final Marker marker, final LatLng toPosition,
                              final boolean hideMarker) {

        Thread background = new Thread() {
            public void run() {

                try {
                    // Thread will sleep for 5 seconds
                    sleep(5*1000);

                    // After 5 seconds redirect to another intent
                    final Handler handler = new Handler();
                    final long start = SystemClock.uptimeMillis();
                    Projection proj = mMap.getProjection();
                    Point startPoint = proj.toScreenLocation(marker.getPosition());
                    final LatLng startLatLng = proj.fromScreenLocation(startPoint);
                    final long duration = 500;

                    final Interpolator interpolator = new LinearInterpolator();

                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            long elapsed = SystemClock.uptimeMillis() - start;
                            float t = interpolator.getInterpolation((float) elapsed
                                    / duration);
                            double lng = t * toPosition.longitude + (1 - t)
                                    * startLatLng.longitude;
                            double lat = t * toPosition.latitude + (1 - t)
                                    * startLatLng.latitude;
                            marker.setPosition(new LatLng(lat, lng));

                            if (t < 1.0) {
                                // Post again 16ms later.
                                handler.postDelayed(this, 16);
                            } else {
                                if (hideMarker) {
                                    marker.setVisible(false);
                                } else {
                                    marker.setVisible(true);
                                }
                            }
                        }
                    });

                    //Remove activity
                    finish();

                } catch (Exception e) {

                }
            }
        };

        // start thread
        background.start();

            }
}

Output

enter image description here

Tushar Narang
  • 1,997
  • 3
  • 21
  • 49

1 Answers1

1

You can try setting the animation through marker options. You can use setAnimation()

Or you can use interpolator, an interpolator defines the rate of change of an animation. This allows the basic animation effects (alpha, scale, translate, rotate) to be accelerated, decelerated, repeated, etc. var marker;

public void animateMarker(final Marker marker, final LatLng toPosition,
    final boolean hideMarker) {
    final Handler handler = new Handler();
    final long start = SystemClock.uptimeMillis();
    Projection proj = mGoogleMapObject.getProjection();
    Point startPoint = proj.toScreenLocation(marker.getPosition());
    final LatLng startLatLng = proj.fromScreenLocation(startPoint);
    final long duration = 500;

    final Interpolator interpolator = new LinearInterpolator();

    handler.post(new Runnable() {
        @Override
        public void run() {
            long elapsed = SystemClock.uptimeMillis() - start;
            float t = interpolator.getInterpolation((float) elapsed /
                duration);
            double lng = t * toPosition.longitude + (1 - t) *
                startLatLng.longitude;
            double lat = t * toPosition.latitude + (1 - t) *
                startLatLng.latitude;
            marker.setPosition(new LatLng(lat, lng));

            if (t < 1.0) {
                // Post again 16ms later.
                handler.postDelayed(this, 16);
            } else {
                if (hideMarker) {
                    marker.setVisible(false);
                } else {
                    marker.setVisible(true);
                }
            }
        }
    });
}

Here's a related SO ticket, you can try the work around offered by community: How to animate marker in android map api V2?

TheOldBlackbeard
  • 395
  • 4
  • 22
KENdi
  • 7,576
  • 2
  • 16
  • 31