【问题标题】:How to handle OVER_QUERY_LIMIT [duplicate]如何处理 OVER_QUERY_LIMIT [重复]
【发布时间】:2015-06-07 07:30:32
【问题描述】:

我正在使用谷歌地图来绘制存储在数据库中的各个位置之间的路径。

fiddle

通过 15 个地理点(超过 10 个)时,状态为 OVER_QUERY_LIMIT。

我知道当我们每秒通过超过 10 个地理点时,我们需要给出几毫秒的时间间隔。

我的问题是如何做到这一点..在哪里添加 sleep() 或 setTimeout() 或任何其他时间延迟代码

我已经尝试了所有,最大限度地提供了 SO 提供的所有可能性,但失败了。因为他们只是说在请求之间留出一些时间间隔,但该怎么做?

代码片段:

    var markers = [
                        {
                            "title": 'abc',
                            "lat": '17.5061925',
                            "lng": '78.5049901',
                            "description": '1'
                        },                  


                        {
                            "title": 'abc',
                            "lat": '17.50165',
                            "lng": '78.5139204',
                            "description": '2'
                        },
                        .
                        .
                        .
                        .
                        .
                        {
                            "title": 'abc',
                            "lat": '17.4166067',
                            "lng": '78.4853992',
                            "description": '15'
                        }

         ];


    var map;

    var mapOptions = {
        center: new google.maps.LatLng(markers[0].lat, markers[0].lng),
        zoom: 15    ,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    var path = new google.maps.MVCArray();
    var service = new google.maps.DirectionsService();

    var infoWindow = new google.maps.InfoWindow();
    var map = new google.maps.Map(document.getElementById("map"), mapOptions);

    var poly = new google.maps.Polyline({
        map: map,
        strokeColor: '#000000'
    });

    var lat_lng = new Array();

    for (var i = 0; i <= markers.length-1; i++)
    {
           var src = new google.maps.LatLng(markers[i].lat, markers[i].lng);
           var des = new google.maps.LatLng(markers[i+1].lat, markers[i+1].lng);

            poly.setPath(path);
            service.route({
                origin: src,
                destination: des,
                travelMode: google.maps.DirectionsTravelMode.DRIVING
            }, 
           function (result, status)
           {
                if (status == google.maps.DirectionsStatus.OK)
                {
  for (var i = 0, len = result.routes[0].overview_path.length;
       i < len; i++) 
                    {
                        path.push(result.routes[0].overview_path[i]);

                    }
                 } 
                 else{

                     if (status == google.maps.GeocoderStatus.OVER_QUERY_LIMIT)
                     {      
                          document.getElementById('status').innerHTML +="request failed:"+status+"<br>";
                     }                                    

            } 
         });

    }
  });

结果图(OVER_QUERY_LIMIT):

【问题讨论】:

  • 我已经提到它@Andreas,但它没有提到 如何做 这完全是关于 做什么 这对于我来假装。

标签: javascript google-maps


【解决方案1】:

谷歌推荐:

通过优化应用程序以更有效地使用网络服务来降低使用率。

在可能的情况下,通过为您的 Google Maps API for Work 许可证购买额外配额来提高使用限制。

另外HERE 有一些解决方法似乎适合您的需要

if status == "OVER_QUERY_LIMIT": 
    time.sleep(2)

这是用 Phyton 编写的,但您可以将其用作 Pseudocode,它易于阅读:

url = "MAPS_API_WEBSERVICE_URL"
attempts = 0
success = False

while success != True and attempts < 3:
  raw_result = urllib.urlopen(url).read()
  attempts += 1
  # The GetStatus function parses the answer and returns the status code
  # This function is out of the scope of this example (you can use a SDK).
  status = GetStatus(raw_result)
  if status == "OVER_QUERY_LIMIT":
    time.sleep(2)
    # retry
    continue
  success = True

if attempts == 3:
  # send an alert as this means that the daily limit has been reached
  print "Daily limit has been reached"

在您的 javascript 代码中,只需设置一个超时时间:

if (status == google.maps.GeocoderStatus.OVER_QUERY_LIMIT)
{      
    setTimeout(3000);
}      

查看working fiddle

【讨论】:

  • 非常感谢您的时间和回复@Jordi..我正在尝试在 JScript 中实现您的概念..我会告诉你什么时候完成它
  • 如果您有任何疑问,请告诉我...我可以帮您处理javascript 代码;)
  • 如果您在 jscript 中编辑您的答案,该答案可以与此 Fiddle 一起使用,那将有很大帮助.. :)
  • 您在我的答案更新中有工作小提琴和相关代码行...希望对您有所帮助...
  • 您好@Jordi...非常感谢您的耐心等待,但很抱歉,路径仍然不完整。您可以看到源(绿点)和目的地(红色-dot) 不满足。路径线在到达目的地之前停止
猜你喜欢
  • 2014-06-20
  • 2021-04-22
  • 1970-01-01
  • 2016-05-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多