Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move markers in google map v2 android

I am doing map clustering in Android Google maps v2. I just want to animate the marker from one geopoint to another. Is there a way to move a marker in Google maps v2?

like image 660
Sandeep Dhull Avatar asked Dec 05 '12 16:12

Sandeep Dhull


People also ask

How do I drag a marker on Google Maps Android?

Long press the marker to enable dragging. When you take your finger off the screen, the marker will remain in that position. Markers are not draggable by default. You must explicitly set the marker to be draggable either with MarkerOptions.

How do I move a marker smoothly on Google Maps?

The transition() and moveMarker() are used to move marker smoothly on click on the Google map.

How do you move a map under a marker?

There is an app named UBER and they provide this feature, you don't drag and drop the marker you just move the map and the marker remained it's position. As you can see green marker is on the center of screen when we move the map it remained its place but shows the new position when the user stop moving the map.


1 Answers

There's one example of moving marker in google map v2 demo app .. in the sample of the play library!!

I have looked into that!! here the code for moving an 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);                     }                 }             }         });     } 

Hope it help every one!!

like image 129
Sandeep Dhull Avatar answered Oct 06 '22 04:10

Sandeep Dhull