【问题标题】:Variable overwritten by ajax response由 ajax 响应覆盖的变量
【发布时间】:2020-09-08 07:40:32
【问题描述】:

使用 Google 地图和地理编码器,我尝试遍历地址对象,为它们返回 LatLng 地址,并在下面的 setMarker 函数中使用原始详细信息和 latlng 地址创建标记。

问题是,response[a] 被对象中的最后一个地址覆盖,因为 for 循环在 AJAX 结果返回之前运行。

如何保存当前循环的response[a] 中的数据,以便以后调用setMarker() 时,它包含正确的信息?

谢谢

          var limit = 0;

          for (a in response){

            if(limit<5){ // limit API calls

                  var addr = [response[a].Addr1, response[a].City, response[a].Zip];

                  geo = new google.maps.Geocoder();
                  geo.geocode({
                    address: addr.join(", "),
                    componentRestrictions: {
                    //  country: 'UK'
                    }
                  }, function (results, status) {

                    if (status == google.maps.GeocoderStatus.OK && results) {

                        var latitude = results[0].geometry.location.lat();
                        var longitude = results[0].geometry.location.lng();
                        var latlng = new google.maps.LatLng(latitude, longitude);

                        if(latitude!="" && longitude!=""){

                            bounds.extend(latlng);
                            map.fitBounds(bounds);
                            _this.setMarker(map, limit, latlng, response[a]);

                        }

                    } // if geo results

              });

            }

            limit++;

          }

【问题讨论】:

    标签: javascript google-maps google-geocoder


    【解决方案1】:

    您面临的问题是一个可以使用闭包函数解决的经典问题。

    当前代码看起来像:

    var a[20];
    
    for(i=0;i<20;i++) {
        some_async_method() {
            //code that uses 'a[i]'
        }
    }
    

    使用闭包在异步函数中保留 var a 的范围:

    var a[20];
    
    for(i=0;i<20;i++) {
        (function(_a){
            some_async_method() {
                //code that uses 'a[i]' as '_a'
            }   
        })(a[i]);// self calling function that preserves the scope of a[i]
    }
    

    所以您的代码将如下所示:

    var limit = 0;
    
    for (a in response){
    
    if(limit<5){ // limit API calls
    
          var addr = [response[a].Addr1, response[a].City, response[a].Zip];
    
          geo = new google.maps.Geocoder();
          (function(response_a){ // closure function to preserve scope of 'response[a]' 
              geo.geocode({
                address: addr.join(", "),
                componentRestrictions: {
                //  country: 'UK'
                }
              }, function (results, status) {
    
                if (status == google.maps.GeocoderStatus.OK && results) {
    
                    var latitude = results[0].geometry.location.lat();
                    var longitude = results[0].geometry.location.lng();
                    var latlng = new google.maps.LatLng(latitude, longitude);
    
                    if(latitude!="" && longitude!=""){
    
                        bounds.extend(latlng);
                        map.fitBounds(bounds);
                        _this.setMarker(map, limit, latlng, response_a);
    
                    }
    
                } // if geo results
    
          });
        })(response[a]);
    
    }
    
    limit++;
    
    }
    

    【讨论】:

    • 你美丽的人。 (我必须更正 reponse_a 的错字,但除此之外效果非常好……我学到了一些东西)非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-27
    • 2017-02-25
    • 2021-08-24
    • 2020-09-30
    • 2015-12-04
    • 2013-06-06
    相关资源
    最近更新 更多