Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript NodeJs&;谷歌地理编码器API_Javascript_Node.js_Web Services_Google Maps Api 3_Get - Fatal编程技术网

Javascript NodeJs&;谷歌地理编码器API

Javascript NodeJs&;谷歌地理编码器API,javascript,node.js,web-services,google-maps-api-3,get,Javascript,Node.js,Web Services,Google Maps Api 3,Get,我正在使用NodeJs制作一个地理编码webapp。地理编码的东西运作良好,除了我有40%的错误620从谷歌,所以我失去了很多地址geocod 错误620:因为http.get(..)使get请求对于Google web服务来说太快 我尝试使用setTimeout(requestGeocod(place,client,details),1000),但NodeJS通常会触发requestGeocod 我可以更改什么以获得100%的请求 /*GOOGLE MAPS GEOCODING API

我正在使用NodeJs制作一个地理编码webapp。地理编码的东西运作良好,除了我有40%的错误620从谷歌,所以我失去了很多地址geocod

错误620:因为http.get(..)使get请求对于Google web服务来说太快

我尝试使用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");
})

}

也许可以在保持100%成功率的情况下,尝试一下每秒可以发出多少请求。然后,每当您需要地理编码时,将请求添加到队列中,并让一些队列执事从中删除内容并调用google(即使用setInterval)。当特定请求完成时,通知客户机队列中的请求附加了一些回调

我猜问题是由于谷歌限制了他们的api使用率(以避免恶意利用)。
您可以做的是创建geoRequest的队列执行器,该执行器将具有以下方法:

  • 排队-将geo请求插入队列尾部
  • 出列-从队列头中删除geo请求
  • configure-我建议接受json对象,该对象在列表中包含一个waitInterval,该waitInterval定义了每个出列任务之间的等待时间
  • 启动和停止(如果要停止)-这将启动队列侦听
  • 执行将执行您的实际任务。
    下面是一个代码示例(我没有检查它,所以在第一次运行时可能无法工作)

    //对于这样的队列模块,我们将调用queue exec
    变量队列=[]//实际队列;
    var区间=1000//默认等待时间
    /**
    *创建QueueExecutor的构造函数。请注意,此代码仅支持一个队列。
    *为了获得更多信息,您可能需要在上面的'queue'变量中保存队列映射。
    *请注意,它是一个JS对象,因此您需要在需要模块后创建它,例如:
    *var qe=require('./queue exec');
    *var myQueue=new QueueExecutor({interval:2000});
    *现在只需使用myQueue添加任务。
    */
    exports.QueueExecutor=函数(配置){
    if(配置和配置间隔)
    间隔=配置。间隔;
    //…如果需要,请在此处添加更多
    }
    QueueExecutor.prototype.enqueue=函数(项){
    if(queue&&item)//您可能还需要检查此处的队列是否过大
    队列。推送(项目);
    }
    QueueExecutor.prototype.dequeue=函数(){
    
    如果(!queue | |(queue.length)请求的速率是多少?
    setTimeout(requestGeocod(place,client,details),1000)
    不改变速率,它只会增加客户端的延迟。客户端的速率非常高。我在客户端使用setTimeout每1秒调用一次requestGeocod(place,client,details)
    //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
    }