-1
var i;
var marker = [];
var infowindow = [];

for(i=0; i<results.length ; i++ ){
marker[i] = new google.maps.Marker({
  position: results[i].geometry.location,
  map: map
});

infowindow[i] = new google.maps.InfoWindow({
  content: "test" + i
});

google.maps.event.addListener(marker[i], 'click', function(num) {
  return function() {
      infowindow[num].open(map, marker[num]);
  }
}(i)); 

}

I wrote some code to have different info window content when click on each marker (test1, test2, test3....) and the info window will remain on screen even i click another marker.

How to only show one infowindow when click on each marker?

Dreams
  • 8,288
  • 10
  • 45
  • 71
  • possible duplicate of [Google Maps JS API v3 - Simple Multiple Marker Example](http://stackoverflow.com/questions/3059044/google-maps-js-api-v3-simple-multiple-marker-example) – geocodezip Aug 01 '15 at 03:16

1 Answers1

1

Just remember the currently opened window, and close it when you need to open another one, similar to this:

var curInfoWindow;
google.maps.event.addListener(marker[i], 'click', function(num) {
  return function() {
    if (curInfoWindow != null) {
        curInfoWindow.close();
    }
    curInfoWindow = infowindow[num];

    infowindow[num].open(map, marker[num]);
  }
}(i));
Kai
  • 15,284
  • 6
  • 51
  • 82