Sunday, May 12, 2024
 Popular · Latest · Hot · Upcoming
124
rated 0 times [  129] [ 5]  / answers: 1 / hits: 74164  / 15 Years ago, wed, december 9, 2009, 12:00:00

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?


More From » google-maps

 Answers
938

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.


[#98111] Saturday, December 5, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
deiong

Total Points: 15
Total Questions: 103
Total Answers: 99

Location: Sudan
Member since Thu, May 7, 2020
4 Years ago
deiong questions
Mon, Nov 22, 21, 00:00, 3 Years ago
Tue, Jun 15, 21, 00:00, 3 Years ago
Mon, Dec 21, 20, 00:00, 3 Years ago
Thu, Oct 15, 20, 00:00, 4 Years ago
;