【问题标题】:Google API Geocoding OVER_QUERY_LIMIT , 5 addresses per second?Google API 地理编码 OVER_QUERY_LIMIT ,每秒 5 个地址?
【发布时间】:2016-07-20 02:46:57
【问题描述】:

首先,我没有达到每日 2,500 个地址的上限。

我有 25 个地址,并且我已经在 J​​avaScript 中将每个地址之间的休眠时间设置为 5 秒。在对 18 或 19 个地址进行地理编码后,我总是会收到 OVER_QUERY_LIMIT 错误。其余 7 或 6 个地址始终无法进行地理编码。

Google API 地理编码限制为每秒 5 个地址,还是增加了?

谢谢。

代码

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

【问题讨论】:

  • 你的代码是什么样的?如果你一次开火 25 发然后休眠 5 秒,那将无济于事。
  • 函数 geocodeAddress(geocoder, 地址) { var arrayLength =addresses.length; for (var i = 0; i
  • 地址通过上面复制的循环。当传递每个地址时,我设置了 sleep(5000) ,即 5 秒。睡眠功能有效。
  • sleep()?它是如何实现的?
  • 我复制了上面的地址循环函数和睡眠函数的代码

标签: javascript google-api geocoding


【解决方案1】:

这是一个复杂的问题。异步很难理解。

话虽如此,这里有一个可行的解决方案。我更喜欢基于 Promise 的解决方案,但 Promise 是另一个需要时间来理解的问题。

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

由于这个函数是异步的,你必须异步使用它......因此回调。所以你使用它如下:

geocodeAddresses(geocoder, addresses, function(result){
    // do what you need with the result here
    console.log(result);
});

这是我能做到的最简单的事情。我创建了一个 jsbin 来模拟地理编码器调用并显示真实结果。将throttleTime 更改为更大的数字以使其变慢。

我希望这会有所帮助。

【讨论】:

  • 谢谢,Wainage!但是,它不起作用。我反复尝试只是运行没有时间延迟功能,我发现 Google API 让我对 15 个地址进行地理编码,之后它显示“查询超出限制”。如果使用睡眠功能,将时间设置在 1000 毫秒到 100000 毫秒之间,Google API 有时会对 24 个位置中的 23 个进行地理编码,有时仍只能对 15 个地址进行地理编码。我想我可能会获得多个密钥来对超过 15 个位置进行地理编码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-29
相关资源
最近更新 更多