移除Google地图标记的反向动画?

时间:2012-04-16 17:29:43

标签: javascript google-maps google-maps-api-3 google-maps-markers

我知道我可以动画在Google地图上添加标记,la 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;
};

2 个答案:

答案 0 :(得分:11)

由于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);
        }
    })();
}

这是DEMO:

答案 1 :(得分:-1)

我的想法:

  1. 创建一个飞行标记针的动画GIF,然后消失。
  2. 关于标记删除事件,交换icon以显示GIF
  3. 由于您创建了GIF,因此您应该知道动画时间长度。然后,使用此值调用setTimeout以调用setMap(null)
  4. 它可能会阻止事件传播,这是许多可能的缺点之一。