【问题标题】:Using an Index to For Loop Through Google Maps Geocode API使用索引通过 Google Maps Geocode API 循环
【发布时间】:2018-02-24 18:57:49
【问题描述】:

我正在尝试获取一个名为“标记”的数组的 javascript 数组,其中存储了我想在 Google 地图上作为标记添加的地点的名称和街道地址。

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

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

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]) 迭代地从“标记”数组中获取标题,如下所示。我收到“未捕获的类型错误:无法读取未定义的属性 '0'。

var 映射; var 地理编码器;

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

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

    • 感谢您的帮助。我之前尝试自己做类似的事情,但似乎无法使其正常工作。不过,那里确实有效,所以我会回头看看我哪里出错了。
    • 不客气,伙计。很高兴我的回答对您有所帮助;)
    • 我不知道你是否仍然活跃,但如果你是,请查看这篇文章吗? stackoverflow.com/questions/53643205/…
    • 你不知道找到这个解决方案是多么的轻松。谢谢。
    • 很高兴能帮到你:)
    猜你喜欢
    • 2021-07-22
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多