raklos
raklos

Reputation: 28555

infowindows on pushpins not closing on google maps

Im using google maps api v3. im adding markers by caling this function:

function createMarker(posn, title, html) {
              var marker = new google.maps.Marker ({position:posn, title: title, draggable: false});
              var infowindow = new google.maps.InfoWindow({content: html});
              google.maps.event.addListener(marker, "click", function() {
                   infowindow.open(map,marker);
              }); 
              return marker;
          }

it works okay, the only problem is when i click a pushpin the window opens, but when i click another pushpin the first pushpins infowindow window does not close both infowindows are visible.

Upvotes: 1

Views: 699

Answers (2)

Michal
Michal

Reputation: 13649

You need to keep track of your info windows in an array and close them programmaticaly when a click event fires so using your example

//define a global array
infoWindows = new Array();

//..do your stuff

function createMarker(posn, title, html) {
      var marker = new google.maps.Marker ({position:posn, title: title, draggable: false});
      var infowindow = new google.maps.InfoWindow({content: html});
      //add this infowindow to an array
      infoWindows.push(infowindow);
      google.maps.event.addListener(marker, "click", function() {
         //go through the array and close all open info windows
         for (i=0;i<infoWindows.length;i++) {
             infoWindows[i].setMap(null);
         }
         //open current info window
         infowindow.open(map,marker);
      }); 
      return marker;
}

Upvotes: 0

Chris Edwards
Chris Edwards

Reputation: 3612

Don't know if you solved this, but the way I did it was:

function createMarker(posn, title, html) {
              var marker = new google.maps.Marker ({position:posn, title: title, draggable: false});

              google.maps.event.addListener(marker, "click", function() {
                   infowindow.open(map,marker);
              });
              infowindow = new google.maps.InfoWindow({content: html});
              return marker;
          }

This works, and the windows close when another pin is clicked, but the "X" close button doesn't work...

Upvotes: 0

Related Questions