2015-10-17 38 views
0

我想輸出一個谷歌地圖API地理編碼查詢結果作爲一個console.log返回的結果數量爲基礎(我已經在下面添加6,但希望console.log的數量是可變的):谷歌地圖地理編碼結果到一個數組

$(document).ready(function(){ 

     // Search Submit 
     $("#search").submit(function(event){ 
      event.preventDefault("", function(){ 
        // 
        }); 

      name = $("#search-name").val(); 
      console.log("Search term at submit is: "+name); 

      // Send to Google Geocoding API 

      var geocoder = new google.maps.Geocoder(); 
      geocoder.geocode({ 
         address : name 
        }, function(results, status){ 
         if (status == google.maps.GeocoderStatus.OK) { 
           console.log(results[0]); 
           console.log(results[1]); 
           console.log(results[2]); 
           console.log(results[3]); 
           console.log(results[4]); 
           console.log(results[5]); 
         } else { 
           alert("Geocode was not successful for the following reason: " + status); 
         } 
        } 
      ); 
     }); 
}); 

我一直在尋找,但我無法弄清楚如何去做這件事。

+1

什麼是您的具體問題? –

+0

嗨@MrNeo所以現在如果我搜索說德國,我會得到1個回報,填寫頂部console.log。如果我搜索大街,那麼有100個回報,我想獲得相關數量的結果中的附加控制檯日誌中的信息。所以...我怎麼讓console.logs的數量根據API返回結果的數量來變量? –

+0

你的意思是你想要一個'variable'包含'console.log'?例如:'variable [1] = console.log(result [1]);' –

回答

2

您可以使用一個循環:

function(results, status){ 
    if (status == google.maps.GeocoderStatus.OK) { 
    for (var i = 0; i < results.length; i++) { 
     console.log(results[i]); 
    } 
    } else { 
    alert("Geocode was not successful for the following reason: " + status); 
    }