简体   繁体   English

Google Maps API v3:animation_changed事件触发两次?

[英]Google Maps API v3: animation_changed event firing twice?

I'm creating a map with v3 of the Google Maps JavaScript API. 我正在使用v3的Google Maps JavaScript API创建地图。 I'm dropping Markers on to the map, and opening up InfoWindows for each Marker, as well as adding info panels to a side div, but I don't want the the InfoWindows or the side info panels opening until after the drop animation has completed, so I made an event listener. 我将标记放到地图上,并为每个标记打开InfoWindows,并将信息面板添加到侧面div,但是我不希望InfoWindows或侧面信息面板打开,直到放下动画后完成,所以我做了一个事件监听器。

google.maps.event.addListener(newMarker, 'animation_changed', function() {
    if (this.animation == null) {
        // Open InfoWindow
        // Add side info panel
    }
});

The problem is that this listener fires twice when the Marker has finished dropping, and the if test evaluates true both times. 问题在于,当Marker完成删除操作后,此侦听器将触发两次,并且两次if评估都为true。 This isn't a problem for the InfoWindow; 对于InfoWindow来说,这不是问题。 once it's open, it's open, and calling .open() on it a second time won't do anything. 一旦打开,它就会打开,并且第二次调用.open()不会执行任何操作。 But the info div is getting added to the side panel twice per event, and that is a problem. 但是每个事件两次将div信息添加到侧面板,这是一个问题。 For now, I created a pretty simple workaround: 现在,我创建了一个非常简单的解决方法:

google.maps.event.addListener(newMarker, 'animation_changed', function() {
    if (this.animation == null && this.dropped != true) {
        this.dropped = true;
        // Open InfoWindow
        // Add side info panel
    }
});

That's having my intended effect, but it feels pretty hacky. 那已经达到了我的预期效果,但是感觉很hacky。 Is there a better way to go about doing this? 有没有更好的方法可以做到这一点? Am I calling the listener incorrectly? 我打听者的电话不正确吗?

Haha, I totally didn't think of that; 哈哈,我完全没有想到这一点。 thanks, Dr.Molle. 谢谢,莫尔博士。 Just stop listening after the first time it fires. 第一次触发后就停止收听。

var myListener = google.maps.event.addListener(newMarker, 'animation_changed', function() {
    google.maps.event.removeListener(myListener);
        if (this.animation == null) {
        // Open InfoWindow
        // Add side info panel
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM