[英]Google API Geocoding OVER_QUERY_LIMIT , 5 addresses per second?
First of all, I didn't reach the maximum daily limit of 2,500 addresses. 首先,我没有达到每天2,500个地址的最大限制。
I have 25 addresses, and I already set the sleep time in JavaScript between each address to 5 seconds. 我有25个地址,并且已经在JavaScript中将每个地址之间的睡眠时间设置为5秒。 I always get OVER_QUERY_LIMIT error after 18 or 19 addresses are geocoded. 在对18或19个地址进行地理编码后,我总是会收到OVER_QUERY_LIMIT错误。 The rest 7 or 6 address always not get geocoded. 其余的7或6地址始终不会进行地理编码。
Google API Geocoding as limit of 5 addresses per second, or they have increased? Google API地理编码限制为每秒5个地址,还是增加了?
Thanks. 谢谢。
Code 码
function geocodeAddress(geocoder, addresses ) {
var arrayLength = addresses.length;
for (var i = 0; i < arrayLength; i++) {
var address = String(addresses[i]);
// alert(address)
sleep(5000)
geocoder.geocode({'address': address}, function (results, status)
{
if (status === google.maps.GeocoderStatus.OK) {
var result = results[0].geometry.location;
var name = results[0].formatted_address;
writeFile(geocode_file_path, name + ',' + result.toString());
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
}
}
function sleep(milliseconds) {
var start = new Date().getTime();
for (var i = 0; i < 1e7; i++) {
if ((new Date().getTime() - start) > milliseconds){
break;
}
}
}
This is a complex problem. 这是一个复杂的问题。 Async is very tricky to get your head around. 异步非常棘手。
Having said that, here's a solution that would work. 话虽如此,这是一个可行的解决方案。 I would prefer a Promise based solution but Promises are another issue that takes time to understand. 我希望使用基于Promise的解决方案,但是Promises是另一个需要花费时间来理解的问题。
function geocodeAddresses(geocoder, addresses, callback){
var result = [];
// this internal function does the hard work
function geocode(address) {
// get timestamp so we can use it to throttle later
var lastCall = Date.now();
// call google function asynchronously
geocoder.geocode({'address': address}, function (results, status) {
// process the return data here
if (status === google.maps.GeocoderStatus.OK) {
result.push({
location: results[0].geometry.location,
address: results[0].formatted_address
});
} else {
result.push({ error: status });
}
// check to see if there are any more addresses to geocode
if (addresses.length) {
// calculate when next call can be made. 200ms is 5 req / second
var throttleTime = 200 - (lastCall - Date.now());
setTimeout(function(){ // create timeout to run in 'throttletime` ms
// call this function with next address
geocode(addresses.shift());
}, throttleTime);
} else { // all done return result
callback(result);
}
});
}
// start the process - geocode will call itself for any remaining addresses
geocode(addresses.shift());
}
Since this function is asynchronous, you have to use it asynchronously ... hence the callback. 由于此函数是异步的,因此您必须异步使用它…因此是回调。 So you use it as follows: 因此,您可以按以下方式使用它:
geocodeAddresses(geocoder, addresses, function(result){
// do what you need with the result here
console.log(result);
});
This is about as simple as I can make it. 这就是我所能做到的。 I've created a jsbin that mocks out the geocoder call and shows real results. 我创建了一个jsbin ,它模拟了geocoder调用并显示了实际结果。 Change the throttleTime to bigger numbers to see it slow down. 将油门时间更改为更大的数字以查看其速度变慢。
I hope this helps. 我希望这有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.