Javascript添加到变量
当我单击下面的代码时,Javascript添加到变量,javascript,for-loop,Javascript,For Loop,当我单击下面的代码时,locationsgohere显示为空白,当我再次单击它时,locationsgohere显示它应该显示的数据 例如,假设我在文本区id中有英国伦敦,这应该将输出显示为变量位置=[['London,51.511214,-0.119824]],,但前提是我单击两次。第一次单击时,它只显示var locations=[], 如果我点击它三次,它只会显示以下var位置=[['London,51.511214,-0.119824]['London,51.511214,-0.1198
locationsgohere
显示为空白,当我再次单击它时,locationsgohere
显示它应该显示的数据
例如,假设我在文本区id
中有英国伦敦
,这应该将输出显示为变量位置=[['London,51.511214,-0.119824]],
,但前提是我单击两次。第一次单击时,它只显示var locations=[],
如果我点击它三次,它只会显示以下var位置=[['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位置在此处,输出;
$('.generate')。单击(函数(){
var temp_addresses=document.getElementById(“gps”).value.split(“\n”);
对于(var i=0;i而言,您遇到的问题是
- 当调用传递给异步
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);
}
var temp_addresses=document.getElementById(“gps”).value.split(“\n”);
var todo=temp_addresses.length;//计算剩余的请求
对于(var i=0;i这是因为我们不知道异步地理代码回调中的代码何时运行
geocoder.geocode(... function() {
// This code will run at some point...
// either before, or after the "code below"
});
// code below
您需要检测循环中的最后一个回调何时触发,然后显示位置。geocode()
是异步的,因此您的输出位于错误的位置…我不熟悉geocode()
,但我假设它是异步的。因此,您的输出需要进入回调函数。我尝试了此操作,并且locationsgohere
在输出中返回为空,即使我将使用我正在使用的新脚本更新原始帖子using@Beardy我想我不清楚。我详细说明了要使用的代码。小心使用out精确地输出我所说的位置(或在从该点调用的函数中)。
geocoder.geocode(... function() {
// This code will run at some point...
// either before, or after the "code below"
});
// code below