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

标签 javascript google-maps-api-3 asynchronous listener

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

我想知道是否有更“合法”或更有效的方法来做到这一点,也许使用 jQuery 延迟对象或 .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;     

最佳答案

使用 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 并行模块的完整文档,请访问 user guidelibrary API 。该文档很小且易于使用。希望这有帮助!

关于Javascript:监听多个异步 Google Maps API v3 请求以完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19277425/

相关文章:

javascript - jQuery判断输入文件是否为空

javascript - 如何使用 jquery 将包含所有元素的数组传递给 php

google-maps-api-3 - 在Google Map中禁用键盘导航

google-maps - Google Places API - 从详细信息响应中获取超过 10 张照片?

javascript - jQuery .load 刷新导致页面缩小

jquery - 如何删除谷歌地图 API 3 的折线

asynchronous - 如何将变量传递给异步函数?

java - 在可完成的 future 之内/之后抛出异常

java - 动态列表从未加载 - 使用 parse.com

javascript - 为什么 `' onhashchange' in window` 在 IE8 的 IE7 兼容模式下返回 `true`?