使用索引进行循环 Google 地图地理编码 API

Using an Index to For Loop Through Google Maps Geocode API

我正在尝试获取一个名为 "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" 来迭代创建这些标记。如果我尝试使用 (title: markers[i][0]) 迭代地从 "markers" 数组中获取标题,如下所示。我收到“未捕获的类型错误:无法读取未定义的 属性 '0'。

可变地图; 变种地理编码器;

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"?

我可以建议你使用闭包吗?

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));
         }
}