-3
我正在使用Google的Geocoder API,並使用下面的循環來達到速率限制。減緩這種情況的最佳方法是什麼?數組長度各不相同,但不超過50個項目。控制循環速度的最佳方法是什麼?
for (var key in data) {
var results = data[key];
var address = results['Address'];
//test
if (geocoder) {
geocoder.geocode({ 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (status != google.maps.GeocoderStatus.ZERO_RESULTS) {
map.setCenter(results[0].geometry.location);
var infowindow = new google.maps.InfoWindow(
{ content: '<b>'+address+'</b>',
size: new google.maps.Size(150,50)
});
var marker = new google.maps.Marker({
position: results[0].geometry.location,
map: map,
title:address
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.open(map,marker);
});
} else {
alert("No results found");
}
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
['setInterval'(https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers.setInterval)在谷歌地圖API V3的[OVER_QUERY_LIMIT – Pointy 2014-10-09 00:31:44
可能重複:如何在Javascript中暫停/延遲以減慢速度?](http://stackoverflow.com/questions/11792916/over-query-limit-in-google-maps-api-v3-how-do-i-暫停延遲在JavaScript的到sl) – geocodezip 2014-10-09 00:40:07
@Pointy - 謝謝。我想過,但不確定這是否合適。 – 2014-10-09 00:48:21