Question

function showpopup(lat, lng, info)
    {

        // Create infoWindow
        var infoWindow = new google.maps.InfoWindow({
            content: info
        });

    // Create marker
        var marker = new google.maps.Marker({
            position: new google.maps.LatLng(lat, lng),
            title: 'Lima',
            map: map.map,
            infoWindow: infoWindow
        });

    // This opens the infoWindow
        infoWindow.open(map, marker);
    }

I have this function which is take three parameter and create the info window at the Map but now i want when user open second info window the first one close using javascript and Gmap V3

Was it helpful?

Solution

You can have a global variable to keep track of the infowindow and close it before opening new one using the close() method, something like

var infoWindow; //global tracker
function showpopup(lat, lng, info)
{
 //close info window in it exists   
if(infoWindow)
infoWindow.close();
// Create new infoWindow
     infoWindow = new google.maps.InfoWindow({
        content: info
    });

// Create marker
    var marker = new google.maps.Marker({
        position: new google.maps.LatLng(lat, lng),
        title: 'Lima',
        map: map.map,
        infoWindow: infoWindow
    });

// This opens the infoWindow
    infoWindow.open(map, marker);
}

If there are more then one info windows, you can have an array, loop through them and call the close() method on each item..

OTHER TIPS

try this code..

var infowindows = new Array();
var popup = new google.maps.InfoWindow({
  // size: new google.maps.Size(420,130),
    content:content_info
});
infowindows.push(popup); //push all infoboxes to an array

google.maps.event.addListener(marker, 'click', function() {
    close_popups();    //close any infobox open
    map.panTo(mycenter1);
    popup.open(map, marker); //opens current marker's infowindow


    // currentPopup = null;
  });

function close_popups(){
  for(var i = 0; i<infowindows.length; i++){
    infowindows[i].close();
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top