Determine if an InfoWindow is open for a particular marker?

7,573 views
Skip to first unread message

Paul

unread,
Aug 2, 2011, 12:35:43 AM8/2/11
to Google Maps JavaScript API v3
I am trying to determine the 'state' of the InfoWindow that is mapped
to a particular Marker. Essentially, in the
google.maps.event.addListener() call, I'd like to check to see if the
InfoWindow is already open, and if it is, close it.

Is this possible?

Thanks!

geoco...@gmail.com

unread,
Aug 2, 2011, 1:06:43 AM8/2/11
to Google Maps JavaScript API v3
Why wouldn't it be? Might not be easy, but doesn't seem hard.

Needs better definition though. Where is the map that you are
intending to do this on?
Are you going to allow multiple infowindows to be open at a time?

-- Larry


>
> Thanks!

Paul

unread,
Aug 2, 2011, 1:18:10 AM8/2/11
to Google Maps JavaScript API v3
No, have a 'global' InfoWindow variable so that only one can be open
at a time.

Glad to hear you think it's possible, just didn't see anything in the
API reference that seemd to be useful in terms of determining
InfoWindow state (something like InfoWindow.isOpen())

The map is loaded onto the same page that I will be trying to
determine the state of the InfoWindow, and I have access to the
variables that are being used to hold the Map, Markers, marker content
and the InfoWindow.

Paul

On Aug 2, 1:06 am, "geocode...@gmail.com" <geocode...@gmail.com>
wrote:

Chris Green

unread,
Aug 2, 2011, 3:26:00 AM8/2/11
to Google Maps JavaScript API v3
The global infowindow is exactly what I did. So in the JS library I'm
using I have:

var infowindow; // the (only) infowindow

in my "loadMap" function I have:

// make the (only) infowindow object, add event on map: close the
infowindow
infowindow = new google.maps.InfoWindow();
google.maps.event.addListener(
map,
'click',
function(){
infowindow.close();
}
);

The "google.maps.event.addListener" is to allow the user to close the
infowindow by clicking anywhere on the background.
Then when I set a marker, I have the following:

google.maps.event.addListener(
marker,
'click',
function(){
infowindow.close();
infowindow.setContent("<b>whatever HTML to show for this
marker<b>");
infowindow.open(map, marker);
}
);

Seems to work (for example):
http://www.allianz-suisse.ch/portal/site/allianz-suisse/menuitem.483ce1c6c2995bcdae6c621073a08a0c/?vgnextoid=2536b25e9aad0310VgnVCM1000005509a8c0RCRD&lang=de&inputZip=8002&inputCity=&Search=suchen

Alex Franco

unread,
Aug 2, 2011, 10:19:50 AM8/2/11
to Google Maps JavaScript API v3
If you have a global variable, it's easy, I wrote a post about that,
hope could help you, is in spanish, but, only what you need is the
code, check it
Post
http://jafrancov.com/2010/09/single-infowindow/
Example
http://jafrancov.com/demos/single-infowindow.php

The central idea: write a click listener for the map variable, so,
when you click anywhere (different from the marker of course), call
the function close of the infowindow variable.

--
Alex Franco

Paul

unread,
Aug 2, 2011, 3:41:26 PM8/2/11
to Google Maps JavaScript API v3
Thanks for the help guys... it's almost what I need, but specifically,
I am looking for 'toggle' functionality when a marker is clicked, not
the rest of the map. Some piece of code that in the Marker's click
listener can check

if InfoWindow is open
close it
else
open it
end if

On Aug 2, 10:19 am, Alex Franco <jafran...@gmail.com> wrote:
> If you have a global variable, it's easy, I wrote a post about that,
> hope could help you, is in spanish, but, only what you need is the
> code, check it
> Posthttp://jafrancov.com/2010/09/single-infowindow/
> Examplehttp://jafrancov.com/demos/single-infowindow.php

geoco...@gmail.com

unread,
Aug 2, 2011, 4:05:14 PM8/2/11
to Google Maps JavaScript API v3
On Aug 2, 12:41 pm, Paul <pmmen...@gmail.com> wrote:
> Thanks for the help guys... it's almost what I need, but specifically,
> I am looking for 'toggle' functionality when a marker is clicked, not
> the rest of the map. Some piece of code that in the Marker's click
> listener can check
>
> if InfoWindow is open
>  close it
> else
>  open it
> end if

What problem are you having implementing this? I would think given
your responses (that there is only one infowindow open at a time) it
would be pretty straightforward.

one possible approach:
1. give each marker an id
2. save that id in a global variable when the infowindow is opened
3. mark it as "none" when the infowindow is closed
4. in the click listener check if the current marker's infowindow is
active before opening it, if it is, close it.

-- Larry

Paul

unread,
Aug 2, 2011, 11:17:12 PM8/2/11
to Google Maps JavaScript API v3
Thanks, that's the method I ended up using, just wondered if there
were any built-in mechanisms for tracking InfoWindow state.

On Aug 2, 4:05 pm, "geocode...@gmail.com" <geocode...@gmail.com>

Esa

unread,
Aug 3, 2011, 6:20:39 AM8/3/11
to Google Maps JavaScript API v3
infoWindow.getMap() returns null if info window is closed.

So you can use simply:

if (infoWindow.getMap())
Reply all
Reply to author
Forward
0 new messages