0

I use google map api for my personnal web photo gallery. For each pictures with gps coordinates, I add a Maker object on a map and an InfoWindows object associated with the marker to show a thumbnail when I click on the marker with mouse.

My question : How to automatically close an InfoWindows when I click on other Marker ? In another word, when I click on a marker, I want to close all other InfoWindows opened before.

You can see some tests here : http://avbo7291.o2switch.net/outils/test-google-map-api.html

Thanks for your help

vincent3569
  • 447
  • 1
  • 4
  • 9

1 Answers1

1

You can use a single instance of InfoWindow and just set new content on marker click like so:

var infowindow = new google.maps.InfoWindow();

google.maps.event.addListener(marker1, 'click', function() {
    infowindow.setContent("Hello World 1 !");
    infowindow.open(map, this);
});

google.maps.event.addListener(marker2, 'click', function() {
    infowindow.setContent("Hello World 2 !");
    infowindow.open(map, this);
});

Then you can attach a click event to the map itself to close this infowindow:

google.maps.event.addListener(map, 'click', function(){
    infowindow.close();
});
dimusic
  • 4,123
  • 2
  • 28
  • 31