简体   繁体   中英

Google Maps API v3: animation_changed event firing twice?

I'm creating a map with v3 of the 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.

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. This isn't a problem for the InfoWindow; once it's open, it's open, and calling .open() on it a second time won't do anything. But the info div is getting added to the side panel twice per event, and that is a problem. 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. 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
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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