如何处理OVER_QUERY_LIMIT

时间:2015-04-02 11:02:43

标签: javascript google-maps

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

fiddle

在传递15个地理位置(超过10个)时,状态为OVER_QUERY_LIMIT。

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

我的问题是怎么做...在哪里添加sleep()或setTimeout()或任何其他延时代码

我已经尝试了所有,最大限度地提供所有可能性但是失败了。因为他们只是说在请求之间留出一些时间差距但是如何做到这一点?

代码段:

    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):

OVER_QUERY_LIMIT

1 个答案:

答案 0 :(得分:1)

谷歌推荐:

  

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

     

在可能的情况下,通过为您的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