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