通过Google Maps Geocode API使用索引进行循环

时间:2017-09-15 14:30:58

标签: javascript arrays google-maps google-maps-api-3 google-maps-markers

我正在尝试使用名为“markers”的javascript数组,这些数组存储了我想在Google地图上添加为标记的地点的名称和街道地址。

var markers = [['Name of Place', '123 1st Street New York, NY'], ['Place 2', '122 1st Street, New York, NY']];

我没有问题使地图正确显示,甚至使标记显示从“markers”数组访问的标题。只要我明确地从“markers”数组访问标题。如下图所示:

var map;
var geocoder;

    function initMap(){
             map = new google.maps.Map(document.getElementById('map'), {
                  center: {lat: 34.6760942, lng: -82.8386035},
                  zoom: 13
             });

             geocoder = new google.maps.Geocoder();

             for(i = 0; i < markers.length; i++){
                 geocoder.geocode({'address': markers[i][1]}, function(results, status) {
                  if(status == 'OK'){
                         marker = new google.maps.Marker({
                              map: map,
                              position: results[0].geometry.location,
                              title: markers[0][0]
                         });
                  } else {
                         alert('Geocode was not successful because: ' + status);
                  }
                 });
             }
    }

但是,我希望能够在for循环中使用索引“i”迭代地创建这些标记。如果我尝试使用(标题:markers [i] [0])迭代地从“markers”数组中获取标题,如下所示。我得到一个“未捕获的类型错误:无法读取未定义的属性'0'。

var map; var geocoder;

function initMap(){
         map = new google.maps.Map(document.getElementById('map'), {
              center: {lat: 34.6760942, lng: -82.8386035},
              zoom: 13
         });

         geocoder = new google.maps.Geocoder();

         for(i = 0; i < markers.length; i++){
             geocoder.geocode({'address': markers[i][1]}, function(results, status) {
              if(status == 'OK'){
                     marker = new google.maps.Marker({
                          map: map,
                          position: results[0].geometry.location,
                          title: markers[i][0]
                     });
              } else {
                     alert('Geocode was not successful because: ' + status);
              }
             });
         }
}

由于某种原因,在函数中没有定义索引i。我怎样才能确保在函数内定义“i”?

1 个答案:

答案 0 :(得分:3)

我建议你使用封闭吗?

function geocodeEncapsulation(i) {
    return( function(results, status){
        if(status == 'OK'){
                     marker = new google.maps.Marker({
                          map: map,
                          position: results[0].geometry.location,
                          title: markers[i][0]
                     });
              } else {
                     alert('Geocode was not successful because: ' + status);
              }
    });
}

function initMap(){
         map = new google.maps.Map(document.getElementById('map'), {
              center: {lat: 34.6760942, lng: -82.8386035},
              zoom: 13
         });

         geocoder = new google.maps.Geocoder();

         for(i = 0; i < markers.length; i++){
             geocoder.geocode({'address': markers[i][1]}, geocodeEncapsulation(i));
         }
}