2011-07-21 50 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 
    }