【问题标题】:NodeJs & Google Geocoder APINodeJs 和谷歌地理编码器 API
【发布时间】:2011-07-21 23:51:48
【问题描述】:

我正在使用 NodeJs 制作地理编码 web 应用程序。地理编码的东西运作良好,除了我有 40% 来自谷歌的错误 620,所以我丢失了很多地址给 geocod。

错误 620:因为 http.get(....) 对 Google 网络服务的获取请求太快了。

我尝试使用 setTimeout(requestGeocod(place, client, details), 1000),但 NodeJS 正常触发 requestGeocod。

我可以改变什么来获得 100% 的请求。

    /*GOOGLE MAPS GEOCODING API QUERY*/
function requestGeocod(place, client, details){
var options = {
  host: 'maps.google.com',
  port: 80,
  path: '/maps/geo?'+ query.stringify({"q":place}) +'&output=json&oe=utf8/&sensor=false&key='+api
};

console.log("Requête : " + options.path)

http.get(options, function(res) {
  console.log("Got response: " + res.statusCode);

    res.setEncoding('utf8');

    res.on('data', function(chunk){
        client.emit('result', {"chunk":chunk, "details":details})
    })

    res.on('end', function(){
        console.log("Fin du GET");
    })

}).on('error', function(e) {
  console.log("Got error: " + e.message);
  client.emit("error");
})

}

【问题讨论】:

  • 您的请求率是多少? setTimeout(requestGeocod(place, client, details), 1000) 不会改变速率,它只会增加客户端的延迟
  • 客户端的比率非常高。我在客户端使用 setTimeout 每隔 1 秒调用一次 requestGeocod(place, client, details)

标签: javascript node.js web-services google-maps-api-3 get


【解决方案1】:

我猜这个问题是由于谷歌对其 api 使用率的限制(以避免恶意利用)。
您可以做的是创建一个 geoRequest 的队列执行器,它将具有以下方法:

  1. 入队 - 将地理请求插入队列尾部。
  2. 出队 - 从队列头部移除地理请求。
  3. 配置 - 我建议接受 json 对象,该对象在列表中包含一个 waitInterval,它定义了每个出队任务之间的等待时间。
  4. 启动和停止(如果您想停止) - 这将启动队列侦听
  5. Execute 将执行您的实际任务。
    这是一个代码示例(我没有检查它,所以它可能在第一次运行时不起作用)

    //Example to such a queue module we will call queue-exec
    var queue = []; //Actual queue;
    var interval = 1000;//Default waiting time
    /**
     * Create a constructor of QueueExecutor. Note that this code support only one queue. 
     *  To make more you may want to hold a map of queue in 'queue' variable above.
     * Note that it is a JS object so you need to create it after require the module such as:
     *  var qe = require('./queue-exec');
     *  var myQueue = new QueueExecutor({interval : 2000});
     *  Now just use myQueue to add tasks.
    */
    exports.QueueExecutor = function(configuration){
      if(configuration && configuration.interval)
        interval = configuration.interval;
        //...Add more here if you want
    }
    
    QueueExecutor.prototype.enqueue = function(item){
      if(queue && item)//You may want to check also that queue is not to big here
        queue.push(item);
    }
    
    QueueExecutor.prototype.dequeue = function(){
      if(!queue || (queue.length <= 0))
        return null;
      return queue.shift();
    }
    
    QueueExecutor.prototype.configure.... do the same as in the constructor
    
    QueueExecutor.prototype.start = function(){
      setInterval(this.execute, interval);
    }
    
    QueueExecutor.prototype.execute = function(){
      var item = this.dequeue();
      if(item)
        item(...);//Here item may be your original request
    }
    

【讨论】:

    【解决方案2】:

    也许可以试验一下每秒可以发出多少个请求,同时仍然保持 100% 的成功率。然后,每当您需要进行地理编码时,将请求添加到队列中,并让一些队列守护进程将其移除并调用 google(即使用 setInterval)。当特定请求完成后,通过队列中的请求附加一些回调通知客户端!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-30
      • 2011-08-28
      相关资源
      最近更新 更多