Javascript 无法阻止超过\u查询\u限制

Javascript 无法阻止超过\u查询\u限制,javascript,api,google-maps,google-api,Javascript,Api,Google Maps,Google Api,我试图在公路上行驶,但我遇到了超限的问题。我在oter主题中看到了类似的问题,但它仍然没有解决我的问题。下面我将介绍代码,我将非常感谢您的提示。谢谢 function initMap(startLat, startLng, endLat, endLng) { var directionsService = new google.maps.DirectionsService; var directionsDisplay = new google.maps.Direc

我试图在公路上行驶,但我遇到了超限的问题。我在oter主题中看到了类似的问题,但它仍然没有解决我的问题。下面我将介绍代码,我将非常感谢您的提示。谢谢

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]);
            }
        }

}
函数初始化映射(STARTAT、STARTNG、endLat、endLng){
var directionsService=新的google.maps.directionsService;
var directionsDisplay=新建google.maps.DirectionsRenderer;
var start=new google.maps.LatLng(startat,starting);
var end=new google.maps.LatLng(endLat,endLng);
计算显示路线(开始、结束、方向服务、方向显示);
}
var i;
var j;
无功延迟;
函数calculateAndDisplayRoute(开始、结束、方向服务、方向显示){
方向服务.路线({
来源:start,
目的地:完,
travelMode:google.maps.travelMode.DRIVING
},功能(响应、状态){
if(status==google.maps.directionstatus.OK){
var totaldistance=0;
var route=response.routes[0];
//显示总距离信息。
对于(var i=0;i”;
方向显示。设置方向(响应);
}否则{
if(status==google.maps.GeocoderStatus.OVER\u QUERY\u LIMIT)
j-=2;
其他的
警报(“地理编码因以下原因未成功:“+状态”);
}
})
;
}
google.maps.event.addDomListener(窗口“加载”,initMap);
功能添加标记(lat、lon、optionMarker)
{
optionMarker.position=新的google.maps.LatLng(lat,lon);
var marker=新的google.maps.marker(optionMarker);
}
函数mapStart()
{
var optionMap=
{
中心:新google.maps.LatLng(50.2719118.86805),
缩放:10,
mapTypeId:google.maps.mapTypeId.ROADMAP
}
map=new google.maps.map(document.getElementById(“map”),optionMap);
对于(i=0;i
很明显,您达到了Maps JavaScript API的每会话方向服务限制

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

你重复地通过向谷歌服务器发出指示请求的点进行循环。这种循环行为会使客户端和Google之间的会话过载

您应该在超过查询限制状态的两次尝试之间增加延迟重试。通常,每次尝试时,延迟都会增加一个乘法因子,这种方法称为指数退避()