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?
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?
Google Maps allows you to only have one info window open. So if you open a new window, then the other one closes automatically.
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.
You'll find you answer here http://www.lootogo.com/googlemapsapi3/markerPlugin.html
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);
});
}
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.