Javascript:侦听多个异步 Google Maps API v3 请求以完成
Javascript: Listening for multiple asynchronous Google Maps API v3 requests to finish
我目前正在使用一个setInterval
函数来"侦听"要设置的"就绪"变量,使用多个Google Geocoder回调。
我想知道是否有一种更"合法"或更有效的方法可以做到这一点,也许使用 jQuery 延迟对象或 .ajaxStop((。
我只是不知道这些是如何工作的,或者它们是否与谷歌地理编码器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 (Yahoo User Interface(。
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。文档很小,易于使用。希望这有帮助!
我认为除了
计算请求之外没有其他方法(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');});
}
相关文章:
- 如何在Ionic/Codova应用程序中保护我的Google Maps Javascript V3 API密钥
- 如何使用PHP's'json_decode'关于新的Google Calendar v3 API
- 对谷歌地图v3 API getDetails()函数感到困惑
- 谷歌地图API v3-API脚本压缩/混淆
- Google Maps v3 API-在地图上绘制圆圈
- YouTube V3 API——按持续时间过滤相关视频信息,不;似乎不起作用
- Google地图V3 API关键用法
- Jquery UI + Google Maps v3 API 仅在刷新后加载
- 本地主机的谷歌地图v3 api不起作用
- Strava v3 api JS GET data
- 谷歌地图v3 api地图没有完全加载
- 从谷歌 v3 API 响应中检索前两位数字
- 在谷歌地图v3 API中使用信息窗口进行聚类标记
- Calling infoWindow w/ Google Map V3 API
- jQuery Mobile interferes with Google Maps v3 API
- Youtube v3 API-pageToken没有'不起作用
- 如何将事件处理程序绑定到Google Maps V3 API默认PanControl's单击事件
- Jquery Ajax & Google Maps v3 API Markers - a { or (
- 在谷歌地图v3 API上绘制区域多边形
- 在执行下一个方法之前未完成(javascript jquery google maps v3 api)