-1

当我单击下面的代码时,它显示locationsgohere为空白,当我再次单击它时,它会locationsgohere显示应有的数据。

比如说我London, UKtextarea #id这应该显示outputvar locations = [['London,51.511214,-0.119824]],但只有当我点击两次。我第一次点击它只显示var locations = [],

如果我单击它三遍,它只会显示以下内容var locations = [['London,51.511214,-0.119824]['London,51.511214,-0.119824]],

for我在这个循环中做错了什么吗?

var locationsgohere,output;
$('.generate').click(function(){
    var temp_addresses = document.getElementById("gps").value.split("\n");
    for(var i=0;i<temp_addresses.length;i++){
        addresses.push(temp_addresses[i]);
        geocoder.geocode( { 'address': temp_addresses[i]}, function(response, status) {
            geocode_results[i] = new Array();
            geocode_results[i]['status'] = status;
            var top_location = response[0];
            var lat = Math.round(top_location.geometry.location.lat() * 1000000)/1000000;
            var lng = Math.round(top_location.geometry.location.lng() * 1000000)/1000000;
            geocode_results[i]['lat'] = lat;
            geocode_results[i]['lng'] = lng;
            geocode_results[i]['l_type'] = top_location.geometry.location_type;
            locationsgohere += "['"+top_location.address_components[0].long_name+","+lat+","+lng+"]";
        });
    }
    if (!locationsgohere){
        locationsgohere = '';
    }
    output = 'var locations = ['+locationsgohere+'],';// JavaScript Document
});

更新代码

var temp_addresses = document.getElementById("gps").value.split("\n");
    var todo = temp_addresses.length; // count the remaining requests
    //  for(var i=0;i<temp_addresses.length;i++){
    for(var i=0;i<temp_addresses.length;i++){
        (function(i){ // protect i in an immediately called function
            addresses.push(temp_addresses[i]);
            geocoder.geocode( { 'address': temp_addresses[i]}, function(response, status) {
                geocode_results[i] = new Array();
                geocode_results[i]['status'] = status;
                var top_location = response[0];
                var lat = Math.round(top_location.geometry.location.lat() * 1000000)/1000000;
                var lng = Math.round(top_location.geometry.location.lng() * 1000000)/1000000;
                geocode_results[i]['lat'] = lat;
                geocode_results[i]['lng'] = lng;
                geocode_results[i]['l_type'] = top_location.geometry.location_type;
//              locationsgohere += "['"+top_location.address_components[0].long_name+","+lat+","+lng+"]";
            });
            if (--todo===0) { // finished
                output = 'var locations = ['+(locationsgohere||'')+'],';
            }
            console.log(locationsgohere);
        })(i);
//  var output = 'var locations = ['+locationsgohere+'],';
    }
4

2 回答 2

2

你遇到的问题是

  • 当你传递给异步 geocode函数的回调被调用时,for循环已经结束并且i具有循环结束的值。
  • 如果您过早构建输出,则必须等待所有请求完成

这是一个解决方案:

var temp_addresses = document.getElementById("gps").value.split("\n");
var todo = temp_addresses.length; // count the remaining requests
for(var i=0;i<temp_addresses.length;i++){
    (function(i){ // protect i in an immediately called function
        addresses.push(temp_addresses[i]);
        geocoder.geocode( { 'address': temp_addresses[i]}, function(response, status) {
            geocode_results[i] = new Array();
            geocode_results[i]['status'] = status;
            var top_location = response[0];
            var lat = Math.round(top_location.geometry.location.lat() * 1000000)/1000000;
            var lng = Math.round(top_location.geometry.location.lng() * 1000000)/1000000;
            geocode_results[i]['lat'] = lat;
            geocode_results[i]['lng'] = lng;
            geocode_results[i]['l_type'] = top_location.geometry.location_type;
            locationsgohere += "['"+top_location.address_components[0].long_name+","+lat+","+lng+"]";
            if (--todo===0) { // finished
                output = 'var locations = ['+(locationsgohere||'')+'],';

                // Use output HERE !

            }
        });
        console.log(locationsgohere);
    })(i);
}
于 2013-11-07T16:32:28.290 回答
0

这是因为我们不知道异步地理编码回调中的代码何时运行。

geocoder.geocode(... function() {
  // This code will run at some point... 
  // either before, or after the "code below"
});

// code below

您需要检测循环中的最后一个回调何时触发,然后显示位置。

于 2013-11-07T16:34:59.007 回答