Good to close other info windows

I am currently doing this to create markers for my google map.

function createMarker(posn, title, html) {
            var marker = new google.maps.Marker({ position: posn, title: title, draggable: false });
            marker['infowindow'] = new google.maps.InfoWindow({ content: html });

            infoWindows.push(marker['infowindow']);
            google.maps.event.addListener(marker, "click", function () {
                for (i = 0; i < infoWindows.length; i++) {
                    infoWindows[i].close();
                }
                this['infowindow'].open(map, this);
            });
            return marker;
        }

im doesn't like the for loop, to close the tokens I know that something like this can be done using the same name:

if (infowindow) infowindow.close ();

the reason I use the code as above is because I am doing something like
markers[myPoint]['infowindow'].open(map, markers[myPoint]);else where, (myPoint is a number).

how can i avoid this for a loop to close open infowindows and do it in a glorious way?

0
source share
2 answers

Just save the last open infoWindow in a global variable:

var activeInfoWindow;

function createMarker(posn, title, html) {
    var marker = new google.maps.Marker({ position: posn, title: title, draggable: false });
    marker['infowindow'] = new google.maps.InfoWindow({ content: html });

    infoWindows.push(marker['infowindow']);
    google.maps.event.addListener(marker, "click", function () {
        if ( activeInfoWindow == this['infowindow'] ) {
            return;
        }
        if ( activeInfoWindow ) {
            activeInfoWindow.close();
        }

        this['infowindow'].open(map, this);
        activeInfoWindow = this['infowindow'];
    });
    return marker;
}
+2
source

- InfoWindow, setContent InfoWindow, .

, 10 000+ .

. http://code.google.com/apis/maps/documentation/javascript/reference.html#InfoWindow

infoWindow = new google.maps.InfoWindow();

function createMarker(posn, title, html) {
    var marker = new google.maps.Marker({ position: posn, title: title, draggable: false });
    marker['content'] = html;
    google.maps.event.addListener(marker, "click", function () {
        infoWindow.setContent(this['content']);
        infoWindow.open(map, this);
    });
    return marker;
}
+2

All Articles