how to move a marker on google maps in android studio

Solutions on MaxInterview for how to move a marker on google maps in android studio by the best coders in the world

showing results for - "how to move a marker on google maps in android studio"
Armand
15 Nov 2019
1    public void animateMarker(final Marker marker, final LatLng toPosition,
2            final boolean hideMarker) {
3        final Handler handler = new Handler();
4        final long start = SystemClock.uptimeMillis();
5        Projection proj = mGoogleMapObject.getProjection();
6        Point startPoint = proj.toScreenLocation(marker.getPosition());
7        final LatLng startLatLng = proj.fromScreenLocation(startPoint);
8        final long duration = 500;
9
10        final Interpolator interpolator = new LinearInterpolator();
11
12        handler.post(new Runnable() {
13            @Override
14            public void run() {
15                long elapsed = SystemClock.uptimeMillis() - start;
16                float t = interpolator.getInterpolation((float) elapsed
17                        / duration);
18                double lng = t * toPosition.longitude + (1 - t)
19                        * startLatLng.longitude;
20                double lat = t * toPosition.latitude + (1 - t)
21                        * startLatLng.latitude;
22                marker.setPosition(new LatLng(lat, lng));
23
24                if (t < 1.0) {
25                    // Post again 16ms later.
26                    handler.postDelayed(this, 16);
27                } else {
28                    if (hideMarker) {
29                        marker.setVisible(false);
30                    } else {
31                        marker.setVisible(true);
32                    }
33                }
34            }
35        });
36    }