Google Maps API v3 - infoWindows都具有相同的内容 [英] Google Maps API v3 - infoWindows all have same content

查看:96
本文介绍了Google Maps API v3 - infoWindows都具有相同的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用infoWindows和Google Maps API v3。
最初,我遇到了其他人在打开新窗口时关闭infoWindows的问题。
我以为通过事先定义infowindow解决了这个问题。现在,当我点击一个新标记时它们关闭,但内容相同。
我应该如何重构我的代码以确保内容每次都是正确的 - 并且在给定时间只有一个infoWindow是打开的?




$ b $保罗

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

函数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 =新的google.maps.InfoWindow({
content:contentString
});


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

google.maps.event.addListener(标记,'点击',function(){
if(infowindow){infowindow.close(map,marker); }
infowindow.open(map,marker);
});
}
});

last = start + count;


解决方案

已更新



您打电话给

  infowindow.open(map,marker); 

在jQuery.each迭代中,因此,我认为它会调用迭代中的最后一项。
修改你的代码,这样你就可以在jQuery.each迭代中获得这个。

  var curItem = 1; 
google.maps.event.addListener(aMarker,click,function(idx,theContent){
return function(){
alert(idx); //打印1 marker1,如果(infowindow){
infowindow.close(map,marker);

$ b $ //你的东西...
if(infowindow) b}
infowindow.setContent(theContent);
infowindow.open(map,marker);
}
}(curItem ++,contentString)
);

当您看到return function()时,我正在使用 javascript closure 。我刚刚用这个闭包做了其他的东西。我在之前的回答中摆脱了以前的其他变化。

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 one. I thought to have solved the problem by defining "infowindow" beforehand. 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?

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. 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.

这篇关于Google Maps API v3 - infoWindows都具有相同的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆