5

我知道我可以为谷歌地图上标记的“添加”设置动画,例如https://developers.google.com/maps/documentation/javascript/overlays#MarkerAnimations

无论如何我可以做反向动画来从地图上删除标记吗?我希望它在移除标记时飞回地图顶部......这可能吗?

到目前为止,这是我的删除代码(只是将其从地图中删除,没有动画):

// TODO figure out if there is a way to animate this removal, like the add
$.contextualMap.prototype.removeMarker = function(m) {
  m.mapMarker.setMap(null);
  m.mapMarker = null;
};
4

2 回答 2

13

由于google.maps.Animation不支持下降的反向动画,您需要编写自己的脚本来为标记设置动画。

你可以这样写:

function removeMarkerWithAnimation(map, marker){
    (function animationStep(){
        //Converting GPS to World Coordinates
        var newPosition = map.getProjection().fromLatLngToPoint(marker.getPosition());

        //Moving 10px to up
        newPosition.y -= 10 / (1 << map.getZoom()); 

        //Converting World Coordinates to GPS 
        newPosition = map.getProjection().fromPointToLatLng(newPosition);
        //updating maker's position
        marker.setPosition( newPosition );
        //Checking whether marker is out of bounds
        if( map.getBounds().getNorthEast().lat() < newPosition.lat() ){
            marker.setMap(null);
        }else{
            //Repeating animation step
            setTimeout(animationStep,10);
        }
    })();
}

这是演示:

于 2012-04-16T18:17:04.297 回答
-1

我的想法:

  1. 创建一个飞行标记别针的动画 GIF,然后消失。
  2. 在标记删除事件上,交换icon以显示 GIF
  3. 由于您创建了 GIF,您应该知道动画时间长度。然后,用这个值 setTimeout 调用 setMap(null)

它可能会阻止事件传播,这是许多可能的缺点之一。

于 2012-04-16T17:41:16.123 回答