2012-07-10 86 views
0

我不得不從地址經緯度發現,我要加載所有這些在全球陣列varibale, 這裏我在做什麼谷歌地圖地理編碼器,它訪問全局變量

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

    var paddress = locations[i]['address'] +','+locations[i]['postal_code'] + ', ' +locations[i]['district'] + ',' + locations[i]['country']; 
    var pgeocoder = new google.maps.Geocoder(); 
    pgeocoder.geocode({ 'address': paddress}, function(results, status) { 
     if (status == google.maps.GeocoderStatus.OK) { 

      Storelatlong[i] = results[0].geometry.location; 

     } 
    }); 
    } 


    alert(Storelatlong.length); 

但它總是返回長度爲0,如果我警告裏面的地理編碼器函數,它給latlong罰款,是否有任何其他方式來從地理編碼器內將數據分配到全局變量,謝謝

回答

0

我認爲問題是,當你得到從谷歌我沒有定義任何更多的響應,嘗試用封閉修復它,像這樣

pgeocoder.geocode({ 'address': paddress}, (function(index) { 
     return function(results, status) { 
     if (status == google.maps.GeocoderStatus.OK) { 
      Storelatlong[index] = results[0].geometry.location; 
     }; 
     }(i)) 
); 
+0

沒有成功仍然給長度爲0 – 2012-07-10 06:48:40

+0

你調試呢?什麼是指數的時響應函數正在運行的價值? – user273895 2012-07-10 17:56:39