views:

863

answers:

5

I need to open only one InfoWindow on my Google Map. I need to close any other InfoWindow before i open a new one.

Can you some one guide me on this?

A: 

Google Maps allows you to only have one info window open. So if you open a new window, then the other one closes automatically.

Kimmo Puputti
that is not right, i can open as many as 20 infowindows on my map. it is v3 btw.
leo
Okay, thanks for correcting. I didn't know it had changed in version 3.
Kimmo Puputti
A: 

You need to keep track of your previous InfoWindow object and call the close method on it when you handle the click event on a new marker.

N.B It is not necessary to call close on the shared info window object, calling open with a different marker will automatically close the original. See Daniel's answer for details.

Cannonade
@Cannonade: I know this is an old answer, and the v3 API was obscure at the time... But actually there's no need to call the `close()` method, if a single `InfoWindow` object is used. It will close automatically if the `open()` method is called again on the same object.
Daniel Vassallo
@daniel-vassallo Thanks for the note :) I have upvoted your answer accordingly, it is the most useful I think.
Cannonade
Thanks @Cannonade :)
Daniel Vassallo
A: 

You'll find you answer here http://www.lootogo.com/googlemapsapi3/markerPlugin.html

bjornhr
A: 

Create your infowindow out of the scope so that you can share it.

Here is a simple example:

var markers = [AnArrayOfMarkers];
var infowindow = new google.maps.InfoWindow();

for (var i = 0, marker; marker = markers[i]; i++) {
  google.maps.event.addListener(marker, 'click', function(e) {
    infowindow.setContent('Marker position: ' + this.getPosition());
    infowindow.open(map, this);
  });
}
skarE
+2  A: 

You need to create just one InfoWindow object, keep a reference to it, and reuse if for all the markers. Quoting from the Google Maps API Docs:

If you only want one info window to display at a time (as is the behavior on Google Maps), you need only create one info window, which you can reassign to different locations or markers upon map events (such as user clicks).

Therefore, you may simply want to create the InfoWindow object just after you initialize your map, and then handle the click event handlers of your markers as follows. Let's say you have a marker called someMarker:

google.maps.event.addListener(someMarker, 'click', function() {
   infowindow.setContent('Hello World');
   infowindow.open(map, someMarker);
});

Then the InfoWindow should automatically close when you click on a new marker without having to call the close() method.

Daniel Vassallo