Google Maps API, all markers opening the same infowindow

后端 未结 2 1388
再見小時候
再見小時候 2021-01-24 02:35

I\'ve got a page that retrieves a bunch of locations and some data about their associated markers and puts them on a Google Maps map. Each one is supposed to pop up its own litt

相关标签:
2条回答
  • 2021-01-24 03:08

    Daff is right about the scope. One alternative, however, is to use bindInfoWindowHtml() instead of a listener with openInfoWindowHtml(). Just replace the listener with this code:

    marker.bindInfoWindowHtml(mess);
    


    UPDATE: As a sidenote - because of closure, any variable created in a for loop will be remembered. A direct solution to the problem is to make a separate function to create the Listener.

    Replace the listener with

    createMarker(marker,mess);
    

    and add the function:

    function createMarker(marker, mess) {
        GEvent.addListener(marker, "click", function() {
            marker.openInfoWindowHtml(mess);
        });
    }
    
    0 讨论(0)
  • 2021-01-24 03:11

    I had this once, too. It is a scoping issue. I had to change my structure a little bit but maybe in your case changing the callback could already help:

    GEvent.addListener(marker, "click", function() {
     marker.openInfoWindowHtml(pins[i].getElementsByTagName("message")[0].childNodes[0].nodeValue;);
    });
    
    0 讨论(0)
提交回复
热议问题