Google Maps API v3 - infoWindows all have same content

前端 未结 1 494
半阙折子戏
半阙折子戏 2021-01-25 17:57

I\'ve been having problems with the infoWindows and Google Maps API v3. Initially, I\'ve ran into the problem that everyone else has of closing infoWindows when opening a new on

1条回答
  •  走了就别回头了
    2021-01-25 18:33

    UPDATED

    You are calling

    infowindow.open(map,marker);
    

    inside an jQuery.each iteration, thus, i think it will be calling the last item in the iteration. Modify your code so you get this inside the jQuery.each iteration.

    var curItem = 1;   
    google.maps.event.addListener(aMarker, "click", function(idx, theContent) {
       return function() {
           alert(idx);  //Should print 1 marker1, 2 for marker 2, to show it's ok.
    
           //Your stuff...
           if (infowindow) { 
              infowindow.close(map,marker); 
           }       
           infowindow.setContent(theContent);  
           infowindow.open(map,marker);
       }
    } (curItem++, contentString)
    );
    

    When you see "return function()" I'm using a javascript closure. I've just used this closure for other stuff. I've got rid of other previous variations in my previous answer.

    0 讨论(0)
提交回复
热议问题