Android-maps-utils: How can I animate a Marker when I put it on the map?

Created on 10 Jun 2015  路  7Comments  路  Source: googlemaps/android-maps-utils

Something like a small bounce or similar.
I don't find the way to get the Marker's view.
If I find the way to get the view, I guess it is easy to apply an animation using View.setAnimation() and res/animator/marker_animation.xml

Any help or suggestion?

Most helpful comment

Bouncing a marker looks like this:

private void animateMarker(final Marker marker) {
    final Handler handler = new Handler();

    final long startTime = SystemClock.uptimeMillis();
    final long duration = 300; // ms

    Projection proj = mMap.getProjection();
    final LatLng markerLatLng = marker.getPosition();
    Point startPoint = proj.toScreenLocation(markerLatLng);
    startPoint.offset(0, -10);
    final LatLng startLatLng = proj.fromScreenLocation(startPoint);

    final Interpolator interpolator = new BounceInterpolator();

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

            if (t < 1.0) {
                // Post again 16ms later (60fps)
                handler.postDelayed(this, 16);
            }
        }
    });
}

All 7 comments

Did you find any solution?

See https://gist.github.com/barbeau/50bb996c8c7c5b9d4ea0 for moving markers from one point to another. It's based on code from Google (see https://gist.github.com/broady/6314689 and https://www.youtube.com/watch?v=WKfZsCKSXVQ&feature=youtu.be). I've been meaning to throw this into a PR to be included in this library, just haven't gotten around to it.

Bouncing a marker looks like this:

private void animateMarker(final Marker marker) {
    final Handler handler = new Handler();

    final long startTime = SystemClock.uptimeMillis();
    final long duration = 300; // ms

    Projection proj = mMap.getProjection();
    final LatLng markerLatLng = marker.getPosition();
    Point startPoint = proj.toScreenLocation(markerLatLng);
    startPoint.offset(0, -10);
    final LatLng startLatLng = proj.fromScreenLocation(startPoint);

    final Interpolator interpolator = new BounceInterpolator();

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

            if (t < 1.0) {
                // Post again 16ms later (60fps)
                handler.postDelayed(this, 16);
            }
        }
    });
}

In the iOS we have two methods in marker.appearAnimation. one is push and another one is pop. But those are not there in Android. Please comment if your not understand. @barbeau

@sixscorpions There is no built-in animation function in the Android Maps API v2. As a developer you're responsible for creating any animation. Can you add a GIF here of how this looks on iOS?

Hi @barbeau Please check this link. https://developers.google.com/maps/documentation/ios-sdk/
In that go to Custom Marker and click on that video. See how marker appearing.

@shankarpilli Definitely nothing in Android Maps API v2 to do this. To do this type of animation I think you'd need to supply your own series of drawables and swap them out in rapid succession. Not sure what the performance would look like for that.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dadino picture dadino  路  7Comments

TatyanaRTB picture TatyanaRTB  路  3Comments

programmeraditya picture programmeraditya  路  5Comments

Slake07 picture Slake07  路  3Comments

mengoni picture mengoni  路  4Comments