从地理编码循环之外访问数组 [英] Accessing array from outside of geocode loop
问题描述
我已成功将地址解码为10个地址,并在地图上添加了一个标记,但我需要访问geocode(codeAddress)函数之外的地理编码结果。我想我可以将结果推送到全局变量数组(userLat,userLng)。使用警报我可以看到循环确实将结果添加到函数中的数组,但数组在函数外是无价值的。
在附注中,警告不成功的地理编码应该返回地址'我'取决于哪个地址有问题,但只显示循环的最后地址值。
也许问题是相关的?我认为这可能与我对嵌套函数缺乏理解有关。
我希望这很清楚。
函数codeAddress(){
var useradd = [];
var geocoder = new google.maps.Geocoder();
var howmany = parseFloat(document.getElementById(howmany)。value);
for(var i = 0; i< howmany; i ++){
useradd [i] = document.getElementById('address ['+ i +']')。
geocoder.geocode({address:useradd [i]},function(results,status){
if(status == google.maps.GeocoderStatus.OK){
userLat.push( parseFloat(results [0] .geometry.location.lat()));
userLng.push(parseFloat(results [0] .geometry.location.lng()));
var marker = new google.maps.Marker({
map:map,
position:results [0] .geometry.location
});
}
else {
alert('地址'+ i +'未成功定位,原因如下:'+ status);
}
});
};
}
编辑:
我不确定我是否正确理解,但已提取回调函数并创建了由地理编码器调用的新函数。问题仍然是相同的,而变量userLat和userLng不在循环之外。任何建议吗?
函数codeAddress(){
函数callBack ){
return function(results,status){
if(status == google.maps.GeocoderStatus.OK){
userLat.push(parseFloat(results [0] .geometry.location .lat()));
userLng.push(parseFloat(results [0] .geometry.location.lng()));
var marker = new google.maps.Marker({
map:map,
position:results [0] .geometry.location
});
}
其他{
alert('地址'+ i +'未成功定位,原因如下:'+ status);
}
}
}
var userLat = [];
var userLng = [];
var useradd = [];
var geocoder = new google.maps.Geocoder();
var howmany = parseFloat(document.getElementById(howmany)。value);
for(var i = 0; i< howmany; i ++){
useradd [i] = document.getElementById('address ['+ i +']')。
geocoder.geocode({address:useradd [i]},callBack());
};
c> codeAddress 函数返回, geocoder.geocode
仍然在做它的工作,它是异步。当 geocoder.geocode
完成时,它会调用它作为匿名函数传递的回调函数。
我建议的解决方案是使回调函数的参数为 codeAddress
,并根据该回调(或从回调中调用的其他函数)执行所需的任何操作,而不是使用全局变量方法。
I have managed to geocode upto 10 address and add a marker to the map but I need to access the geocode results outside of the geocode (codeAddress) function. I thought I could push the results to a global variable array (userLat, userLng). Using alerts I can see that the loop does indeed add the results to the array within the function but the array is valueless outside of the function.
On a side note, the alert for unsuccessful geocoding should return Address 'i' depending which address had the problem but only displays the last address value of the loop.
Maybe the problems are related? I think it may have something to do with my lack of understanding of the nested function.
I hope this is clear. Thanks in advance for any help!
function codeAddress() {
var useradd = [];
var geocoder = new google.maps.Geocoder();
var howmany = parseFloat(document.getElementById("howmany").value);
for (var i=0; i<howmany; i++) {
useradd[i] = document.getElementById('address['+i+']').value;
geocoder.geocode( {address: useradd[i]}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
userLat.push(parseFloat(results[0].geometry.location.lat()));
userLng.push(parseFloat(results[0].geometry.location.lng()));
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
}
else {
alert('Address ' + i + ' was not successfully located for the following reason: ' + status);
}
});
};
}
Edit:
I'm not sure if I understood correctly but have extracted the callback function and created a new function of it that is called by the geocoder. The problem is still the same though and the variables userLat and userLng are not carried outside of the loop. Any suggestions?
function codeAddress() {
function callBack() {
return function(results, status){
if (status == google.maps.GeocoderStatus.OK) {
userLat.push(parseFloat(results[0].geometry.location.lat()));
userLng.push(parseFloat(results[0].geometry.location.lng()));
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
}
else {
alert('Address ' + i + ' was not successfully located for the following reason: ' + status);
}
}
}
var userLat = [];
var userLng = [];
var useradd = [];
var geocoder = new google.maps.Geocoder();
var howmany = parseFloat(document.getElementById("howmany").value);
for (var i=0; i<howmany; i++) {
useradd[i] = document.getElementById('address['+i+']').value;
geocoder.geocode({address: useradd[i]}, callBack());
};
}
When your codeAddress
function returns, geocoder.geocode
is still doing its job, which is asynchronous. When geocoder.geocode
is done, it will invoke its callback, which you're passing as an anonymous function.
The solution I suggest is to make the callback function a parameter of codeAddress
, and do whatever you need from that callback (or from other function called inside the callback), instead of using the global variables approach.
这篇关于从地理编码循环之外访问数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!