Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/450.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Google Maps V3地理编码和循环中的标记_Javascript_Google Maps_Google Maps Api 3_Closures_Geocoding - Fatal编程技术网

Javascript Google Maps V3地理编码和循环中的标记

Javascript Google Maps V3地理编码和循环中的标记,javascript,google-maps,google-maps-api-3,closures,geocoding,Javascript,Google Maps,Google Maps Api 3,Closures,Geocoding,我的代码有一些问题,我在sql数据库中有一个机场列表,我想为每个机场创建标记 对于我得到的每个机场的国际民航组织代码的地址,每个机场的国际民航组织都是独一无二的 我以数组的形式从数据库中获取数据 它被保存在“temp”中,带有一个split函数,并通过for循环将它们逐个获取 地理编码不是问题,但我不知道为什么标题和点击事件会出现 它始终是所使用数组中的最后一个 这是页面,数据库中的最后一个条目是ZBAA 所有的标记都放在正确的位置,但标题是错误的:s 我想问题在于“地址”,但我不确定 for

我的代码有一些问题,我在sql数据库中有一个机场列表,我想为每个机场创建标记

对于我得到的每个机场的国际民航组织代码的地址,每个机场的国际民航组织都是独一无二的

我以数组的形式从数据库中获取数据

它被保存在“temp”中,带有一个split函数,并通过for循环将它们逐个获取

地理编码不是问题,但我不知道为什么标题和点击事件会出现 它始终是所使用数组中的最后一个

这是页面,数据库中的最后一个条目是ZBAA

所有的标记都放在正确的位置,但标题是错误的:s

我想问题在于“地址”,但我不确定

for (var i = 0; i < temp.length; ++i){

     var address=temp[i];

     geocoder.geocode({ 'address': address}, function(results){            
          var marker  = new google.maps.Marker({
              map: map, 
              position: results[0].geometry.location,
              title:address
          });

          google.maps.event.addListener(marker, 'click', function() {
               window.open ('infomonde.php?icao='+address+'&language=fr', 'Informations météo', config='height=400, width=850, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, directories=no, status=no')});
     });  
};
for(变量i=0;i
我猜是因为

geocoder.geocode({ 'address': address}, function(results){ ... });
回调是在同一上下文中执行的

尝试在相同的上下文中执行标记。下面的代码将等待获取所有地理编码器。然后解析到标记

var results = {};
var waiting = temp.length;

while(temp.length > 0){

  var fetching = temp.pop();

  geocoder.geocode(
    { address: fetching}, 
    function(response){
      results[fetching] = response[0].geometry.location;
      --waiting;
      if(waiting == 0) // wait for everything to finish
        setMarker();
    }
  );
}
var setMarker = function(){
  for(var element in results){
    var marker  = new google.maps.Marker({
              map: map, 
              position: results[element],
              title: element
              });

    google.maps.event.addListener(marker, 'click', function() {
    window.open ('infomonde.php?icao='+element+'&language=fr', '', 'configs')});
  }
}
ps window.open如果我没弄错的话,某些浏览器会拒绝弹出标题(并可能导致无法打开弹出标题)。我总是留空。

下面是一个使用“虚拟”地址和警报的示例,用于显示与每个标记关联的正确数据:

在for循环中,您遇到的是一个典型的闭包/范围问题。要解决此问题,请在将
temp[i]
变量传递到geocode和其中的回调函数之前,使用closure对其进行本地化:

    for (var i = 0; i < temp.length; ++i) {
        (function(address) {
            geocoder.geocode({
                'address': address
            }, function(results) {
                var marker = new google.maps.Marker({
                    map: map,
                    position: results[0].geometry.location,
                    title: address
                });

                google.maps.event.addListener(marker, 'click', function() {
                    //alert(address);  //use alert to debug address
                    window.open('infomonde.php?icao=' + address + '&language=fr', 'Informations météo', config = 'height=400, width=850, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, directories=no, status=no')
                });
            });
        })(temp[i]);  //closure passing in temp[i] and use address within the closure
    }
for(变量i=0;i
address=temp[i]
它必须做的是,你传递给
address=temp[i]的i似乎需要做一个闭包,然后传入
address
,但是如果没有JSFIDLE演示,我不能确定你是否介意为address/temp数组提供一些虚拟字段?闭包是怎么回事?你说的虚拟字段是什么意思?如果你的意思是我已经尝试了一些错误的入口,但是地理编码失败了^^@user657848,那么看看我的示例并探索演示。如果您有任何问题,请告诉我,并随时发表评论,以便我保留address=temp[i]?因为你在你的地理代码中使用了它{address:address}改为{address:fetching}我错了