Skip to content Skip to sidebar Skip to footer

All Infowindows Have Same Data

im fairly new to the Google Maps V3 API and i'm running into an issue where all infowindows have the same data, the code im using iterates through an array of marker details and ou

Solution 1:

Definitely use the var declaration for the infowindow; otherwise infowindow is global and you're overwriting it every time you call createMarker().

After that, you just need to close all open infowindows every time you show one. Here's how I've solved this before:

...
var counter = 1,
    infowindows = [];

function closeInfoWindows()
{
    var i = infowindows.length;
    while(i--)
    {
        infowindows[i].close();
    }
}

function createMarker(name, latlng, icon, info) 
{
    var marker = new google.maps.Marker({
        position: latlng,
        map: map,
        icon: icon,
        title: name
    });

    var infowindow = new google.maps.InfoWindow({            
        content: info
    });

    infowindows.push(infowindow);

    google.maps.event.addListener(marker, 'click', function ()
    {
        infowindow.open(map,marker);
    });

    document.getElementById(counter).onclick = function ()
    {
        infowindow.open(map, marker);
    }

    counter++;
    return marker;
}

Post a Comment for "All Infowindows Have Same Data"