简体   繁体   中英

NodeJs & Google Geocoder API

I'm using NodeJs to make a Geocoding webapp. The geocoding things works well except the fact that I have 40% of error 620 from google, so I'm loosing a lot of address to geocod.

Error 620: Because http.get(....) is making the get request too fast for the Google web service.

I tried with a setTimeout(requestGeocod(place, client, details), 1000), but NodeJS fires requestGeocod normaly.

What can I change to get 100% of my request.

    /*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");
})

}

I guess that the problem is due to Google's restriction on their api usage rate (to avoid bad exploitation).
What you can do is to create a queue executor of geoRequest that will have the following methods:

  1. Enqueue - insert a geo request to the queue's tail.
  2. Dequeue - remove geo request from the queue's head.
  3. configure - I recommand to accept json object that contain at list a waitInterval that define how long to wait between each task that is dequeued.
  4. Start and stop (if you want to stop) - that will start the queue listening
  5. Execute that will execute your actual task.
    Here is an example of code (I didn't check it so it might not work on first run)

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

Maybe experiment with how many requests you can make per second while still keeping a 100% success rate. Then, whenever you need to geocode, add the request to a queue, and have some queue deamon taking things off it and calling google (ie with setInterval). When the specific request is finished, notify the client with some callback attached to the request in the queue!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM