2011-07-21 12 views
2

私はNodeJsを使用してジオコーディングWebアプリケーションを作成しています。ジオコーディングは、私がエラー620の40%をGoogleから持っていることを除いてうまくいくので、私はジオコードに多くのアドレスを失っています。NodeJs&Google Geocoder API

エラー620:http.get(....)が、Google Webサービスに対してgetリクエストを非常に速くしているためです。

私は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"); 
}) 

} 

事前にお願い致します。

+0

(それが最初の実行では動作しない場合がありますので、私はそれをチェックしませんでした)コードの例ですか? 'setTimeout(requestGeocod(place、client、details)、1000)'はレートを変更せず、クライアントの待ち時間を追加するだけです –

+0

クライアントは非常に高速です。クライアント側のsetTimeoutを使用して1秒ごとにrequestGeocod(場所、クライアント、詳細)を呼び出しました。 – Servernumber

答えて

1

100%の成功率を維持しながら、1秒あたりのリクエスト数を試してみることもできます。次に、ジオコードを作成する必要があるときはいつでも、リクエストをキューに追加して、デーモンキューをオフにしてGoogleを呼び出す(setIntervalを使用して)。特定の要求が完了したら、キュー内の要求にコールバックを添付してクライアントに通知します。

3

問題は、悪意のある悪用を避けるため、GoogleのAPI使用率の制限によるものだと思います。
何ができることは、次の方法がありますgeoRequestのキューexecutorを作成することです:

  1. エンキュー - キューの最後尾に地理要求を挿入します。
  2. デキュー - キューの先頭からジオリクエストを削除します。
  3. configure - デキューされている各タスク間で待機する時間を定義する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 
    } 
    
関連する問題