Google Map v2 Marker Animation
Solution 1
I found a solution that worked for me:
final LatLng target = NEW_LOCATION;
final long duration = 400;
final Handler handler = new Handler();
final long start = SystemClock.uptimeMillis();
Projection proj = map.getProjection();
Point startPoint = proj.toScreenLocation(marker.getPosition());
final LatLng startLatLng = proj.fromScreenLocation(startPoint);
final Interpolator interpolator = new LinearInterpolator();
handler.post(new Runnable() {
@Override
public void run() {
long elapsed = SystemClock.uptimeMillis() - start;
if (elapsed > duration) {
elapsed = duration;
}
float t = interpolator.getInterpolation((float) elapsed / duration);
double lng = t * target.longitude + (1 - t) * startLatLng.longitude;
double lat = t * target.latitude + (1 - t) * startLatLng.latitude;
marker.setPosition(new LatLng(lat, lng));
if (t < 1.0) {
// Post again 10ms later.
handler.postDelayed(this, 10);
} else {
// animation ended
}
}
});
Solution 2
You are welcome to change the position of a Marker
at any point by calling setPosition()
. You are welcome to change the position of the "camera" (i.e., the map's center and zoom level) at any point by applying a CameraUpdate
object using moveTo()
or animateTo()
on GoogleMap
. Combining these with a light timing loop (e.g., using postDelayed()
) should allow you to achieve a similar animation effect.
GrIsHu
Android Developer Design, Develop and maintain applications(web,mobile), softwares. Develop and test database-driven and object-oriented web applications and web services. Develop and test sophisticated, cross-browser front-end HTML/CSS/Javascript Web GUIs. Develop, configure and maintain database tables for driven applications.
Updated on June 09, 2022Comments
-
GrIsHu almost 2 years
Do anyone have idea how this animation's implementation is possible in google map api v2. Check out this here. I would like to know how this is done. Please let me know if anyone have any sample code regarding this.
Thanks in advance.
-
GrIsHu over 11 yearsCan you please provide some code so that i can get better idea of implementation. I have tried to animate the map using CameraUpdateFactory.scrollBy method.. its working fine even. But not found any way of animating the on the path on the map.
-
CommonsWare over 11 years@Grishu: "Can you please provide some code so that i can get better idea of implementation" -- I do not have any lying around that handles your scenario. "But not found any way of animating the on the path on the map" -- you have to find "the path on the map" yourself, using some Web service (presumably), then update the
Marker
positions to be various points along that path. -
GrIsHu over 11 yearsOk, Thank you for your help.
-
Keval Patel over 8 yearsInterpolator interpolator = new LinearInterpolator(); This line giving error ... "Incompatible type"
-
apmartin1991 over 8 yearsmake sure you import: import android.view.animation.Interpolator; import android.view.animation.LinearInterpolator; And not the other Interpolator.
-
Master over 7 yearsi found a bug. Is necessary to add this two lines: if (elapsed>duration) elapsed=duration;