jbdemonte / gmap3

jQuery plugin to create Google maps
http://gmap3.net
668 stars 198 forks source link

Closing all existing infowindow when you click a marker to open a new one #150

Closed adam-jones-net closed 5 years ago

adam-jones-net commented 5 years ago

This took me ages to work out so I'm posting here to share as I don't believe this is documented anywhere on the gmaps3 official docs...

var infoWindows = [];
var locations=[
    {position:[123,456],content:'<h3>marker title 1</h3><p>msg text</p>/div>'}
]
var map=$('#mapName').gmap3()
.marker(locations)
.infowindow(locations)
.then(function (infowindow) {
    var map = this.get(0);
    var marker = this.get(1);
    marker.forEach(function(item,i){
        item.addListener('click', function() {
            closeAllInfoWindows();
            infowindow[i].open(map, item);
            infoWindows.push(infowindow[i]);
        });
    })
})
.fit();

function closeAllInfoWindows() {
  for (var i=0;i<infoWindows.length;i++) {
     infoWindows[i].close();
  }
}