【问题标题】:Can not block OVER_QUERY_LIMIT无法阻止 OVER_QUERY_LIMIT
【发布时间】:2017-04-30 19:43:55
【问题描述】:

我尝试开车上路,但我遇到了 OVER_QUERY_LIMIT 的问题。我在其他主题中看到了类似的问题,但它仍然没有解决我的问题。下面我介绍代码,我将非常感谢提示。谢谢

function initMap(startLat, startLng, endLat, endLng) {
        var directionsService = new google.maps.DirectionsService;
        var directionsDisplay = new google.maps.DirectionsRenderer;
        var start = new google.maps.LatLng(startLat, startLng);
        var end = new google.maps.LatLng(endLat, endLng);
        calculateAndDisplayRoute(start, end, directionsService, directionsDisplay);
    }
    var i;
    var j;
    var delay;
    function calculateAndDisplayRoute(start, end, directionsService, directionsDisplay) {
        directionsService.route({
            origin: start,
            destination: end,
            travelMode: google.maps.TravelMode.DRIVING
        }, function(response, status) {
            if (status === google.maps.DirectionsStatus.OK) {
                var totaldistance = 0;
                var route = response.routes[0];
                // display total distance information.
                for (var i = 0; i < route.legs.length; i++) {
                    totaldistance = totaldistance + route.legs[i].distance.value;
                }
                document.getElementById('distance').innerHTML += "<p>total distance is " + (totaldistance / 1000).toFixed(2) + " km</p>";
                directionsDisplay.setDirections(response);
            } else {
                if (status == google.maps.GeocoderStatus.OVER_QUERY_LIMIT)
                    j -= 2;
                else
                    alert("Geocode was not successful for the following reason: " + status);
            }
        })
        ;
    }
    google.maps.event.addDomListener(window, "load", initMap);
    function addMarker(lat,lon,optionMarker)
    {
        optionMarker.position = new google.maps.LatLng(lat,lon);
        var marker = new google.maps.Marker(optionMarker);
    }

    function mapStart()
    {
        var optionMap =
            {
                center: new google.maps.LatLng(50.27191 ,18.86805),
                zoom: 10,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            }
        map = new google.maps.Map(document.getElementById("map"), optionMap);
        for(i=0; i < points.length; i += 2)
        {
            addMarker(points[i],points[i+1],{});
            for(j = i+2; j < points.length; j += 2 ) {
                initMap(points[i], points[i + 1], points[j], points[j + 1]);
            }
        }

}

【问题讨论】:

    标签: javascript api google-maps google-api


    【解决方案1】:

    显然,您正在达到 Maps JavaScript API 路线服务的每次会话限制。

    无论有多少用户共享同一个项目,服务请求都受每个用户会话的速率限制。首次加载服务 API 时,会为您分配初始请求配额。使用此配额后,API 会按每秒对其他请求实施速率限制。如果在某个时间段内发出过多请求,API 将返回 OVER_QUERY_LIMIT 响应代码。每个会话的速率限制阻止将客户端服务用于批处理请求。

    https://developers.google.com/maps/documentation/javascript/usage

    您反复循环通过向 Google 服务器发出路线请求的点。这种循环行为可能会使您的客户端和 Google 之间的会话过载。

    您应该在 OVER_QUERY_LIMIT 状态的尝试之间增加延迟重试。通常,每次尝试都会使延迟增加一个乘法因子,这种方法称为指数退避 (https://en.wikipedia.org/wiki/Exponential_backoff)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-19
      • 1970-01-01
      • 2015-01-15
      • 2017-07-16
      • 2012-02-27
      • 2012-06-30
      相关资源
      最近更新 更多