javascript 谷歌地图在有限时间内在标记上弹跳动画

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14657779/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 22:15:48  来源:igfitidea点击:

Google Maps Bounce Animation on Marker for a limited period

javascripthtmlgoogle-mapsgoogle-maps-api-3

提问by cgval

I wish that a marker bounces for a few seconds and eventually stops automatically.. I am trying this code :

我希望标记弹跳几秒钟并最终自动停止..我正在尝试以下代码:

1. globalMarkers[i].setAnimation(google.maps.Animation.BOUNCE);
2. setTimeout(function() {
3.    globalMarkers[i].setAnimation(null)
4. }, 3000);

but for some reason, line 1 executes (hence marker will start bouncing) but the 3rd line returns the following error:

但由于某种原因,第 1 行执行(因此标记将开始弹跳)但第 3 行返回以下错误:

Uncaught TypeError: Cannot call method 'setAnimation' of undefined
        (anonymous function)

Any ideas what it could be?

任何想法可能是什么?

回答by ScottE

This works fine (with a single global marker object)

这工作正常(使用单个全局标记对象)

    marker.setAnimation(google.maps.Animation.BOUNCE);

    setTimeout(function() {
        marker.setAnimation(null)
    }, 3000);

My guess is that you're interating, and your setTimeout i is not in scope. Try this instead:

我的猜测是您正在交互,并且您的 setTimeout i 不在范围内。试试这个:

    for (var x = 0; x < 5; x++) {
        var marker = markers[x];
        marker.setAnimation(google.maps.Animation.BOUNCE);
        stopAnimation(marker);
    }


function stopAnimation(marker) {
    setTimeout(function () {
        marker.setAnimation(null);
    }, 3000);
}

There are some more creative solutions here:

这里有一些更有创意的解决方案:

Javascript how to use setTimeout on an iterative list operation?

Javascript 如何在迭代列表操作中使用 setTimeout?