Search code examples
javascriptnode.jsweb-servicesgoogle-maps-api-3get

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

}

Solution

  • 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!