gpt4 book ai didi

javascript - 如何处理 OVER_QUERY_LIMIT

转载 作者:行者123 更新时间:2023-11-28 00:26:50 24 4
gpt4 key购买 nike

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

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

OVER_QUERY_LIMIT

最佳答案

Google 推荐:

Lowering usage, by optimizing applications to use the web services more efficiently.

Increasing usage limits, when possible, by purchasing additional allowance for your Google Maps API for Work license.

还有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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com