谷歌地图地理编码结果成阵列

时间:2015-10-17 08:57:16

标签: javascript google-maps-api-3

我正在尝试将基于返回的结果数量的Google Maps API Geocode查询结果输出到一个数组作为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 :(得分: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);
  }