Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Have just one InfoWindow open in Google Maps API v3

I need to have only one InfoWindow open on my Google Map. I need to close all other InfoWindows before I open a new one.

Can someone show me how to do this?

like image 708
leo Avatar asked Dec 09 '09 17:12

leo


People also ask

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!

How do I know if InfoWindow is open?

Calling InfoWindow. getOpenedState() returns a boolean which reflects the state (opened/closed) of the infowindow.

How do you use InfoWindow in maps?

When you create an info window, it is not displayed automatically on the map. To make the info window visible, you must call the open() method on the InfoWindow , passing an InfoWindowOpenOptions object literal specifying the following options: map specifies the map or Street View panorama on which to open.


1 Answers

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, this); }); 

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

like image 116
Daniel Vassallo Avatar answered Sep 18 '22 01:09

Daniel Vassallo