How to animate marker when it is added to map on Android?

后端 未结 3 643
独厮守ぢ
独厮守ぢ 2020-12-14 11:39

I want to animate map markers when they are added to map.

User should see the map with markers around him. Each new marker should bounce.

3条回答
  •  清歌不尽
    2020-12-14 12:10

    Anchor the marker off screen or at your start position then start the animation.

    Note the .setAnchor used in this method was added to the google map api v2 in May 2013

    I've just now got this working for one marker by tweaking the extras samples maps demo and I don't like the performance of this implementation. The most important piece is to anchor the marker off screen or off at your start position. I'm using off screen above.

    Anchor the marker off screen .setAnchor(.5f,(size of screen above marker / size of marker )) //for the map demo perth is about 6f for my test phone. Change the animation to bounce to the same value it was 6f for my test phone.

    private void addMarkersToMap() {
        // A few more markers for good measure.
    mPerth = mMap.addMarker(new MarkerOptions().position(PERTH)
                .title("Perth").snippet("Population: 1,738,800")
                .anchor(.5f, 6f)
                );
    

    Change the animation so it bounces to (size of screen above marker/size of marker) (6f on my test phone). I'm just using the onclick handler because it's already setup to bounce with tweaks bounce to 6f and a longer duration. So after all the markers have been added to the map I fire off the click handler.

    this.onMarkerClick(mPerth);
    

    The changed onMarkerClick handler with the 6f and longer duration.

    @Override
    public boolean onMarkerClick(final Marker marker) {
        if (marker.equals(mPerth)) {
            // This causes the marker at Perth to bounce into position when it
            // is clicked.
            final Handler handler = new Handler();
            final long start = SystemClock.uptimeMillis();
            final long duration = 2500;
    
            final Interpolator interpolator = new BounceInterpolator();
    
            handler.post(new Runnable() {
                @Override
                public void run() {
                    long elapsed = SystemClock.uptimeMillis() - start;
                    float t = Math.max(
                            1 - interpolator.getInterpolation((float) elapsed
                                    / duration), 0);
    
                    marker.setAnchor(0.5f, 1.0f + 6 * t);
    
                    if (t > 0.0) {
                        // Post again 16ms later.
                        handler.postDelayed(this, 16);
                    }
                }
            });
        } else if (marker.equals(mAdelaide)) {
            // This causes the marker at Adelaide to change color.
            marker.setIcon(BitmapDescriptorFactory.defaultMarker(new Random()
                    .nextFloat() * 360));
        }
        // We return false to indicate that we have not consumed the event and
        // that we wish
        // for the default behavior to occur (which is for the camera to move
        // such that the
        // marker is centered and for the marker's info window to open, if it
        // has one).
        return false;
    }
    

    Good Luck

提交回复
热议问题