我需要运行几千个 api 调用才能获得一些距离,但我一直在达到查询限制。即使在我将脚本设置为每 1 秒调用一次后,我仍然会在前 120-170 次调用后得到 QUERY_LIMIT 作为响应。在那之后,他们继续快速堆积。所以过了一会儿,我需要几秒钟甚至几分钟才能得到真正的回应。
该脚本每 1 秒调用一次以获取行驶距离。如果返回 ZERO_RESULT,那么它会在最后添加一个步行距离调用。如果用 ZERO_RESULT 响应,我只需通过坐标计算径向距离。如果它达到查询限制,它会在最后广告另一个相同值的调用。
这是我的代码:
function getDistance( lat1, lon1, lat2, lon2, relId, mode, nr, total ) {
setTimeout( function(){
var directionsService = new google.maps.DirectionsService();
if( mode == 'driving' ) travelmode = google.maps.TravelMode.DRIVING;
else { travelmode = google.maps.TravelMode.WALKING }
var request = {
origin: new google.maps.LatLng( lat1, lon1 ),
destination: new google.maps.LatLng( lat2, lon2 ),
travelMode: travelmode
};
directionsService.route(request, function(result, status) {
if (status == google.maps.DirectionsStatus.OK) {
distance = parseInt( result.routes[0].legs[0].distance.value )/1000;
$.ajaxLoadSync( 'getDistance', SITE_ROOT+'admin/modules/settings/calculateDistance.php', 'lat1='+lat1+'&long1='+lon1+'&lat2='+lat2+'&long2='+lon2+'&relId='+relId+'&distance='+distance );
} else if( status == google.maps.DirectionsStatus.ZERO_RESULTS && mode == 'driving' ){
var walkingCount = 0;
var limitCount = 0;
if( $('#limit_hit').html() != '' ) limitCount = parseInt($('#limit_hit').html());
if( $('#walking_dist').html() != '' ) walkingCount = parseInt($('#walking_dist').html());
var offset = walkingCount + limitCount;
$('#walking_dist').html( walkingCount + 1 );
setTimeout(function() { getDistance( lat1, lon1, lat2, lon2, relId, 'walking', nr, total ) }, total*1000+offset*1000 );
} else if( status == google.maps.DirectionsStatus.ZERO_RESULTS && mode == 'walking' ){
var noResult = 0;
if( $('#no_res').html() != '' ) noResult = parseInt($('#no_res').html());
$('#no_res').html( noResult + 1 );
$.ajaxLoadSync( 'getDistance', SITE_ROOT+'admin/modules/settings/calculateDistance.php', 'lat1='+lat1+'&long1='+lon1+'&lat2='+lat2+'&long2='+lon2+'&relId='+relId+'&distance=-1' );
} else if( status == google.maps.DirectionsStatus.OVER_QUERY_LIMIT ){
var walkingCount = 0;
var limitCount = 0;
if( $('#limit_hit').html() != '' ) limitCount = parseInt($('#limit_hit').html());
if( $('#walking_dist').html() != '' ) walkingCount = parseInt($('#walking_dist').html());
var offset = walkingCount + limitCount;
$('#limit_hit').html( limitCount + 1 );
setTimeout(function() { getDistance( lat1, lon1, lat2, lon2, relId, mode, nr, total ) }, total*1000+offset*1000 );
}
});
}, nr*1000 );
}
我会感谢任何帮助或提示。