2011-07-21 49 views
2

我正在使用NodeJs来制作地理编码web应用程序。地理编码的东西运行良好,除了我有谷歌的错误620的40%的事实,所以我失去了很多地址到地理编码。NodeJs&Google地理编码器API

错误620:因为http.get(....)正在GET请求过快的谷歌Web服务。

我试着用的setTimeout(requestGeocod(地点,客户,细节),1000),但火灾的NodeJS requestGeocod normaly。

我可以改变让我的要求的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"); 
}) 

} 

提前谢谢你。

+0

什么是你请求的速率? 'setTimeout(requestGeocod(place,client,details),1000)'不会改变速率,它只会增加客户端的延迟。 –

+0

客户端的速率非常高。我在客户端使用setTimeout来调用每1sec requestGeocod(地点,客户端,详细信息) – Servernumber

回答

1

有多少可以请求每秒钟做,同时仍保持100%的成功率也许尝试。然后,无论何时您需要进行地理编码,请将请求添加到队列中,并让一些队列取消它并调用google(即使用setInterval)。当特定请求完成时,通过队列中的请求附加一些回叫来通知客户端!

3

我想这个问题是由于谷歌对自己的API使用率限制(避免不良开发)。
你可以做的是创造geoRequest队列执行,将有以下几种方法:

  1. 排队 - 插入地理请求队列尾。
  2. 出队 - 从队列头部删除地理要求。
  3. 配置 - 我推荐接受包含在列表定义多久即出队每个任务之间等待waitInterval JSON对象。
  4. 启动和停止(如果你想停止) - 这将启动队列侦听
  5. 执行,将执行您的实际任务。
    下面是代码的一个例子(我没有检查它,所以它可能无法在第一次运行工作)

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