2

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

我想知道是否有更“合法”或更有效的方法来做到这一点,也许使用 jQuery Deferred Object 或 .ajaxStop()。

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

所以目前我正在设置 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;     
4

2 回答 2

2

使用 YUI(雅虎用户界面)。

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");
});    

});

有关 YUI3 并行模块的完整文档,请访问用户指南库 API。该文档很小且易于使用。希望这可以帮助!

于 2013-10-09T16:37:54.877 回答
1

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

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

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');});
}
于 2013-10-09T18:10:00.933 回答