4

我的代码存在一些问题,我有一个sql数据库中的机场列表,我想为这些机场中的每一个创建标记。谷歌地图V3地理编码和循环中的标记

,因为我得到了国际民航组织代码为每个机场的地址,国际民航组织是每个机场

我从数据库中的数据作为数组独特

它与一个分割保存在“临时”函数和for循环它得到他们1 1 1

地理编码是不是问题,但我不知道为什么TITLE和点击事件 它总是最后一个从数组是用过的。

这里是页面,数据库中的最后一项是ZBAA。

而且所有的标记被放置在正确的位置,但标题是错误的:■

http://mizar.lte.lu/~pr1011_meteo/projet/cartemonde4.php

问题是与“地址”我想,但我不敢肯定。

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')}); 
    }); 
}; 
+0

它所要做的瓦特/您传递了'地址I =温度[I]'好像你需要做一个闭包,并通过'地址',但我不能确定没有放置jsfiddle演示 – kjy112 2011-03-13 20:27:09

+0

你是否介意为地址/临时数组提供一些虚拟域? – kjy112 2011-03-13 20:36:05

+0

关闭是什么?你是什​​么意思与虚拟领域?如果你的意思是说,把一些错误entrys我已经尝试和地理编码失败^^ – user657848 2011-03-13 20:47:08

回答

0

我的猜测是因为

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如果我没有记错的话,一些浏览器拒绝弹出标题(和可能导致无法打开弹出式)。我一直留空。

+0

,所以我应该保持地址=临时[我]?因为你使用它在你的地理编码 – user657848 2011-03-13 20:58:42

+0

{地址:地址}更改为{地址:提取}我的错误 – Bonshington 2011-03-14 04:07:43

10

这里是一个JSFiddle Demo使用“虚拟”地址和警报,以显示与每个标记正确的数据关联:

你有什么是内部的for循环典型的封闭/范围问题。要解决这个问题,使用封闭传递到地理编码和回调函数中之前进行本地化temp[i]变量:

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 
    } 
+0

oh thx帮助球员^^ – user657848 2011-03-15 06:23:18