Google Maps API V3,Listener表现得很奇怪

我创建了一串标记和信息框,我想通过一个听众链接在一起,尽管听者似乎在没有任何点击的情况下在开始时触发,并且如果我试图通过点击标记来触发听众(在关闭框之后)什么也没有发生。

//Load Markers
function LoadData() {
    //Space Reservation
    var site_arr  = new Array();
    var point_arr = new Array();
    var pasta_arr = new Array();
    //Load All Information
    point_arr = [new google.maps.LatLng(38.629343,-9.191592),
                 new google.maps.LatLng(38.649187,-9.189205)];

    site_arr = ["AAA","BBB"];

    pasta_arr = [1,2];

    //Create Markers and Set InfoBoxes
    for(var i = 0 ; i < site_arr.length ; i++){
        marker_arr[i] = new google.maps.Marker({
               position: point_arr[i]
               ,map: map
               ,title: site_arr[i]
                });

            window_arr[i] = new InfoBox({
             content: site_arr[i]
            });
            google.maps.event.addListener(marker_arr[i], 'click', function(i){
           window_arr[i].open(map,marker_arr[i]);
            }(i));
            }
    return 0;
}

任何人都知道发生了什么?

解决了问题,我只是发布它,因为它可能对其他人有用......谢谢大家!

// Load Markers function LoadData(){//空间预留var site_arr = new Array(); var point_arr = new Array(); var pasta_arr = new Array(); //加载所有信息point_arr = [new google.maps.LatLng(38.629343,-9.191592),new google.maps.LatLng(38.649187,-9.189205)];

site_arr = ["AAA",
            "BBB"];

pasta_arr = [1,2];

//Create Markers and Set InfoBoxes
for(i = 0 ; i < site_arr.length ; i++){
    marker_arr[i] = new google.maps.Marker({
        position: point_arr[i]
        ,map: map
        ,title: site_arr[i]
        ,icon: "http://labs.google.com/ridefinder/images/mm_20_red.png"
    });

    marker_arr[i]._info = new InfoBox({
         content: pasta_arr[i];
    });
    attachListener(marker_arr[i]);
}
return 0;

}

函数attachListener(marker){google.maps.event.addListener(marker,'click',function(){marker._info.open(map,marker);}); 返回0; }


您不是将函数添加到您的侦听器,而是一个函数值。 您显示信息窗口是因为您在LoadData()期间在i处评估了侦听器的function(i) LoadData() 。 第二个问题是,即使您删除了评估,您在调用侦听器期间也不会获得预期的索引i 。 正确的方式是这样的:

marker_arr[i]._info = window_arr[i];
google.maps.event.addListener(marker_arr[i], 'click', function() {
    var marker = this;
    marker._info.open(map, marker);
});
链接地址: http://www.djcxy.com/p/96191.html

上一篇: Google Maps API V3, Listener is behaving strangely

下一篇: Google Maps API v3 : Click events not triggered in firefox for custom marker