javascript - 谷歌地图标记删除的反向动画?

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

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

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

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

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

最佳答案

作为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:

关于javascript - 谷歌地图标记删除的反向动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10178789/

相关文章:

javascript - 标签计数器

javascript - 计算行驶距离 Google Maps API

javascript - 在 google map api 的 javascript 中访问数组内部的数组

javascript - 谷歌地图 API 3 : how to solve error of "unexpected token <"?

javascript - 谷歌地图API,如何为多个自定义标记添加标记阴影

javascript - autoprefixer 针对 'last 2 versions' 的目标浏览器是什么?

javascript - 如何在 underscore.js 模板中使用 if 语句?

javascript - 如何使用 javascript 字典中的键查找值

google-maps - 谷歌地图默认 tiptool 倾斜箭头样式

javascript - Google API 转换持续时间