一次一个标记动画 Google Maps Api 3

发布于 2024-11-17 15:30:40 字数 425 浏览 1 评论 0原文

我正在开发地图应用程序,但一次无法使标记动画化。我希望拥有它,以便当单击新标记时,另一个标记的动画将被删除。我是一个 js 菜鸟,一直在绞尽脑汁。截至目前,我将标记放在一个数组中,以便我可以删除动画..但是在为另一个标记设置动画时如何删除每个其他标记的动画?..下面是我一直在播放的代码和

for (i in markersArray) {
if (markersArray[i].getAnimation() != null) {
markersArray[i].setAnimation(null);
}
if (marker[i].getAnimation() != null) {
marker[i].setAnimation(null);
} else {
marker[i].setAnimation(google.maps.Animation.BOUNCE);
}

}

I'm working on a Map app and I'm having trouble getting marker animated at a time. I'd like to have it so that when a new marker is clicked, the other's animation is removed. I'm a js noob and have been wracking my head. As of now, I have the markers in an array so that I can remove the animation ..but how can I remove the animation of every other marker when setting the animation for another one?..below is the code I've been playing with

for (i in markersArray) {
if (markersArray[i].getAnimation() != null) {
markersArray[i].setAnimation(null);
}
if (marker[i].getAnimation() != null) {
marker[i].setAnimation(null);
} else {
marker[i].setAnimation(google.maps.Animation.BOUNCE);
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

巷子口的你 2024-11-24 15:30:40

您发布的代码看起来不错,如果它适合您,那就太好了。但是,如果您一次只想对一个标记进行动画处理,那么仅保留最后一个动画标记的变量可能会更高效、更简单。例如,在点击事件中或添加标记时,您可以执行以下操作:

previous_animation.setAnimation(null);
new_marker.setAnimation(google.maps.Animation.BOUNCE);
previous_animation = new_marker;

The code that you posted seems fine and if it works for you, great. However, if you only want one marker animated at a time, then it might be more efficient, and easier, to just keep a variable of the last animated marker. For example, in your click event or when you add a marker, you could do something like this:

previous_animation.setAnimation(null);
new_marker.setAnimation(google.maps.Animation.BOUNCE);
previous_animation = new_marker;
坐在坟头思考人生 2024-11-24 15:30:40

在为每个标记分配一个唯一的 id 后,我最终想出了以下代码......工作起来就像一个魅力

for( var i in marker ){
marker[i].setAnimation(null);
if( marker[i].id == item.id ) marker[i].setAnimation(google.maps.Animation.BOUNCE);
}

I ended up coming up with the following code after assigning a unique id to each marker...worked like a charm

for( var i in marker ){
marker[i].setAnimation(null);
if( marker[i].id == item.id ) marker[i].setAnimation(google.maps.Animation.BOUNCE);
}
阳光下慵懒的猫 2024-11-24 15:30:40
var new_marker = this;
if(prev_marker) {
  if (prev_marker.getAnimation() != null) {             
      prev_marker.setAnimation(null);                               
      new_marker.setAnimation(google.maps.Animation.BOUNCE);
      prev_marker = new_marker;
  }
} else {
  new_marker.setAnimation(google.maps.Animation.BOUNCE);
  prev_marker = new_marker;
}

我更喜欢@Nick Canzoneri 风格。稍微修改了一下

var new_marker = this;
if(prev_marker) {
  if (prev_marker.getAnimation() != null) {             
      prev_marker.setAnimation(null);                               
      new_marker.setAnimation(google.maps.Animation.BOUNCE);
      prev_marker = new_marker;
  }
} else {
  new_marker.setAnimation(google.maps.Animation.BOUNCE);
  prev_marker = new_marker;
}

i prefer @Nick Canzoneri style. Modified it a bit

给我一枪 2024-11-24 15:30:40

这就是我所做的工作,我将标记放在全局变量中。这使得我的标记可以随时访问。

google.maps.event.addListener(marker, 'click', function(event) {
    $("input#pickup-"+ marker.id +"").prop('checked', true);

    for (var i = window.markers.length - 1; i >= 0; i--) {
        window.markers[i].setAnimation();
        if(window.markers[i].id === this.id) {
            marker.setAnimation(google.maps.Animation.BOUNCE);
        }
    };

})

This is what that i got working, I have my markers in a global variable. Which keeps my markers accessible at all time.

google.maps.event.addListener(marker, 'click', function(event) {
    $("input#pickup-"+ marker.id +"").prop('checked', true);

    for (var i = window.markers.length - 1; i >= 0; i--) {
        window.markers[i].setAnimation();
        if(window.markers[i].id === this.id) {
            marker.setAnimation(google.maps.Animation.BOUNCE);
        }
    };

})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文