Google Map v2 Marker Animation

半城伤御伤魂 提交于 2019-11-27 19:32:51
D-32

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
        }
    }
});

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.

Great news is that he Google Map API v2 gives the new Camera controls. You can check the new features and how to use them directly here on the Youtube channel of the Android developers team.

It also provides animate, tilt, bearing ... but I think the video is very detailed and also talks about applications like the one in your example.

Have fun, and give me a link when you finish your app.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!