Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Maps: Auto close open InfoWindows?

On my site, I'm using Google Maps API v3 to place house markers on the map.

The InfoWindows stay open unless you explicitly click the close icon. Meaning, you can have 2+ InfoWindows open at a time if you hover over the map marker.

Question: How do I make it so that only the current active InfoWindow is open and all other InfoWindows are closed? Meaning, no more than 1 InfoWindow will be open at a time?

like image 387
Ted Avatar asked Feb 08 '10 17:02

Ted


People also ask

What is Google Maps InfoWindow?

An InfoWindow displays content (usually text or images) in a popup window above the map, at a given location. The info window has a content area and a tapered stem. The tip of the stem is attached to a specified location on the map. Info windows appear as a Dialog to screen readers.

How do I edit InfoWindow on Google Maps?

You can modify the whole InfoWindow using jquery alone... var popup = new google. maps. InfoWindow({ content:'<p id="hook">Hello World!


2 Answers

There is a close() function for InfoWindows. Just keep track of the last opened window, and call the close function on it when a new window is created.

like image 71
Chris B Avatar answered Sep 28 '22 21:09

Chris B


alternative solution for this with using many infowindows: save prev opened infowindow in a variable and then close it when new window opened

var prev_infowindow =false;  ... base.attachInfo = function(marker, i){     var infowindow = new google.maps.InfoWindow({         content: 'yourmarkerinfocontent'     });      google.maps.event.addListener(marker, 'click', function(){         if( prev_infowindow ) {            prev_infowindow.close();         }          prev_infowindow = infowindow;         infowindow.open(base.map, marker);     }); } 
like image 30
mike Avatar answered Sep 28 '22 20:09

mike