This question already has answers here:
OVER_QUERY_LIMIT in Google Maps API v3: How do I pause/delay in Javascript to slow it down?
                                
                                    (6个答案)
                                
                        
                        
                            How do I Geocode 20 addresses without receiving an OVER_QUERY_LIMIT response?
                                
                                    (6个答案)
                                
                        
                                4年前关闭。
            
                    
我正在使用谷歌地图绘制存储在数据库中的各种位置之间的路径。

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

最佳答案

Google推荐:


  通过优化应用程序以更有效地使用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

关于javascript - 如何处理OVER_QUERY_LIMIT ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29411338/

10-12 12:38
查看更多