简体   繁体   English

Google Maps API v3-infoWindows都具有相同的内容

[英]Google Maps API v3 - infoWindows all have same content

I've been having problems with the infoWindows and Google Maps API v3. 我在使用infoWindows和Google Maps API v3时遇到了问题。 Initially, I've ran into the problem that everyone else has of closing infoWindows when opening a new one. 最初,我遇到了其他所有人在打开新窗口时都关闭infoWindows的问题。 I thought to have solved the problem by defining "infowindow" beforehand. 我认为已经通过预先定义“ infowindow”解决了这个问题。 Now they close when I click on a new marker, but the content is the same. 现在,当我单击新标记时,它们将关闭,但是内容相同。 How should I re-structure my code to make sure the content is the right one each time - and only one infoWindow is open at a given time? 如何重新组织代码以确保每次内容正确—在给定时间仅打开一个infoWindow?

Thank you! 谢谢!

Paul 保罗

var allLatLngs = new Array();
var last = 0;
var infowindow;

function displayResults(start, count){
    if(start === undefined){
        start = last;
    }
    if(count === undefined){
        count = 20;
    }
    jQuery.each(jsresults, function(index, value) {
        if(index >= start && index < start+count){
            var obj = jQuery.parseJSON(value);
        $("#textresults").append(index + ": <strong>" + obj.name + "</strong> " + Math.round(obj.distanz*100)/100 + " km entfernt" + "<br/>");

            var myLatlng = new google.maps.LatLng(obj.geo_lat, obj.geo_lon);
            allLatLngs.push(myLatlng);

        var contentString = '<strong>'+obj.name+'</strong>';

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


            var marker = new google.maps.Marker({
                  position: myLatlng,
                  //title:"Hello World!"
              });
            marker.setMap(map);

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

    last = start+count;  

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. 在jQuery.each迭代内部,因此,我认为它将调用迭代中的最后一项。 Modify your code so you get this inside the jQuery.each iteration. 修改您的代码,以便将其包含在jQuery.each迭代中。

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 . 当您看到“ return function()”时,我正在使用javascript闭包 I've just used this closure for other stuff. 我刚刚将此封包用于其他内容。 I've got rid of other previous variations in my previous answer. 我在先前的回答中摆脱了其他先前的变化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM