Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/382.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript:侦听多个异步Google Maps API v3请求以完成_Javascript_Google Maps Api 3_Asynchronous_Listener - Fatal编程技术网

Javascript:侦听多个异步Google Maps API v3请求以完成

Javascript:侦听多个异步Google Maps API v3请求以完成,javascript,google-maps-api-3,asynchronous,listener,Javascript,Google Maps Api 3,Asynchronous,Listener,我目前正在使用一个setInterval函数来“侦听”要设置的“ready”变量,使用多个Google Geocoder回调 我想知道是否有一种更“合法”或更有效的方法来实现这一点,也许可以使用jQuery延迟对象或.ajaxStop() 我只是不知道这些是如何工作的,或者它们是否与GoogleGeocoderAPI和回调函数兼容 因此,目前我正在将var,geocodeReady设置为true一旦所有地理编码完成,然后每1000毫秒设置一个间隔函数,以检查geocodeReady是否设置为tr

我目前正在使用一个
setInterval
函数来“侦听”要设置的“ready”变量,使用多个Google Geocoder回调

我想知道是否有一种更“合法”或更有效的方法来实现这一点,也许可以使用jQuery延迟对象或.ajaxStop()

我只是不知道这些是如何工作的,或者它们是否与GoogleGeocoderAPI和回调函数兼容

因此,目前我正在将var,
geocodeReady
设置为
true
一旦所有地理编码完成,然后每1000毫秒设置一个间隔函数,以检查
geocodeReady
是否设置为true

有没有更有效/更快/简洁的方法

谢谢大家!

var geocoder = new google.maps.Geocoder();

var addresses = [
    ['Bondi Beach Australia'],
    ['Coogee Beach Australia'],
    ['Cronulla Beach Australia'],
    ['Manly Beach Australia']
];

var n = addresses.length;
var geocodeReady = false;

for (i = 0; i < addresses.length; i++) {
    (function(address){

        geocoder.geocode( { 'address': addresses[i][0]}, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                address.push(results[0].geometry.location.lb);
                address.push(results[0].geometry.location.mb);

            } else {
                alert("Geocode was not successful for the following reason: " + status);
            }

            if (--n == 0) {
                geocodeReady = true;
            }                                   
        });

    })(addresses[i]);
}

var x = 0;

// set a timer to check whether our addresses have geocoded

function checkGeocode() {

    if(x === 10) {
        clearInterval(geocodeTimer);
    }

    if(geocodeReady == true) {
        clearInterval(geocodeTimer);

        for (i = 0; i < addresses.length; i++) {

            marker = new google.maps.Marker({
                position: new google.maps.LatLng(addresses[i][1], addresses[i][2]),
                map: map
            });

            google.maps.event.addListener(marker, 'click', (function(marker, i) {
                return function() {
                    infowindow.setContent(addresses[i][0]);
                    infowindow.open(map, marker);
                }
            })(marker, i));
        }
    }

    x++;
}

var geocodeTimer = setInterval(checkGeocode, 1000);

geocodeTimer;     
var geocoder=new google.maps.geocoder();
变量地址=[
[“澳大利亚邦迪海滩”],
['Coogee Beach Australia'],
['Cronulla Beach Australia'],
[“澳大利亚曼利海滩”]
];
var n=地址。长度;
var geocodeReady=false;
对于(i=0;i
使用YUI(雅虎用户界面)

YUI()。使用(“并行”,函数(Y){
var stack=newy.Parallel();
对于(i=0;i

有关YUI3并行模块的完整文档,请访问或。文档很小,易于使用。希望这有帮助

我认为除了计算请求之外没有其他方法了(
YUI.Parallel
也这样做)

我猜您希望在所有请求完成后执行某些操作。因此,不要从外部检查变量,而是在
n==0
时执行所需的函数(当您希望更动态时,要执行的函数可以作为参数传递)

for(变量i=0,n=addresses.length;i
您使用的是,
address.push(结果[0].geometry.location.lb)的未记录属性;address.push(结果[0].geometry.location.mb)不要这样做,使用有文档记录的.lat(),.lng()方法。也不确定为什么需要在数组中保存坐标。通常的解决方案是对地址进行地理编码,将回调中的标记和信息窗口添加到地理编码器,而不是保存它们,并在完成所有地理编码后添加标记。
YUI().use("parallel", function(Y){
var stack = new Y.Parallel();
for (i = 0; i < addresses.length; i++) {
    (function(address){

        geocoder.geocode( { 'address': addresses[i][0]}, stack.add(function(results, status) {
            //every callback function will get into the stack.
            if (status == google.maps.GeocoderStatus.OK) {
                address.push(results[0].geometry.location.lb);
                address.push(results[0].geometry.location.mb);
            } else {
                alert("Geocode was not successful for the following reason: " + status);
            }

        }));

    })(addresses[i]);
}

//when all callbacks are done...
stack.done(function(){
     console.log("all geocoding are finished");
});    

});
for (var i = 0,n=addresses.length; i < addresses.length; i++) {
    (function(address,callback){
        geocoder.geocode( { 'address': address[0]}, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                address.push(results[0].geometry.location.lat());
                address.push(results[0].geometry.location.lng());
            } else {
                alert("Geocode was not successful for the following reason: " 
                       + status);
            }
            if (--n === 0) {            
                callback(addresses);
            }                                   
        });

    })(addresses[i],
       function(){console.dir(addresses);alert('geocoding finished');});
}