Javascript 谷歌地图API-超过每秒查询限制
我正在使用谷歌地图API来显示20个地点的列表和到达时间。我正在使用google.maps.DirectionsService。这个API的每秒调用数限制为10次,如果我只有10个位置,那么我可以同时调用所有位置(经过测试,效果非常好)。当我有超过10个位置时,我会使用呼叫块进行呼叫。例如,对于16个位置:我先呼叫前10个位置,然后等待3秒钟,然后呼叫其他6个位置 理论上,这种方法应该有效,因为我尊重每秒10次的限制但是,不起作用。当我尝试调用剩余的6个位置时,我得到了错误,超过了查询限制,有时其中4个失败,有时其中3个失败。如果我将等待时间增加到5秒,有时工作正常,有时其中一个失败 问:这个限制不是应该是每秒10个查询吗?如果是,那么我会做错什么?我怎样才能修好它Javascript 谷歌地图API-超过每秒查询限制,javascript,google-chrome,google-maps-api-3,Javascript,Google Chrome,Google Maps Api 3,我正在使用谷歌地图API来显示20个地点的列表和到达时间。我正在使用google.maps.DirectionsService。这个API的每秒调用数限制为10次,如果我只有10个位置,那么我可以同时调用所有位置(经过测试,效果非常好)。当我有超过10个位置时,我会使用呼叫块进行呼叫。例如,对于16个位置:我先呼叫前10个位置,然后等待3秒钟,然后呼叫其他6个位置 理论上,这种方法应该有效,因为我尊重每秒10次的限制但是,不起作用。当我尝试调用剩余的6个位置时,我得到了错误,超过了查询限制,有时
//------------------ Code ----------------
getDistanceByBlocks(destinations: IDestination[]){
let limit = 8;
if(destinations.length <= limit){
destinations.forEach(destination => {
this.getDistance(destination);
});
}else{
let selection = destinations.slice(0, limit)
let rest = destinations.slice(limit)
selection.forEach(destination => {
this.getDistance(destination);
});
setTimeout(() => {
this.getDistanceByBlocks(rest)
},3000);
}
}
getDistance(destination: IDestination) {
if (this.dataService.getCurrentLocation()) {
let mapSettings = this.userSettings.getMapSettingsSync();
this.mapService.getRoutes(destination, mapSettings);
}
}
//------------------ MapService----------------
getRoutes(destination: IDestination, mapSettings:IMapSettings): void {
let directionsService = new google.maps.DirectionsService;
this.currentLocation = this.dataService.getCurrentLocation();
directionsService.route(
{
origin: { lat: this.currentLocation.latitude,
lng: this.currentLocation.longitude },
destination: { lat: destination.latitude, lng: destination.longitude },
provideRouteAlternatives: mapSettings.provideRouteAlternatives,
avoidHighways: mapSettings.avoidHighways,
avoidTolls: mapSettings.avoidTolls,
drivingOptions: {
departureTime: new Date(Date.now()),
trafficModel: mapSettings.trafficModel
},
travelMode: google.maps.DirectionsTravelMode.DRIVING,
unitSystem: google.maps.UnitSystem.IMPERIAL
},
(response, status) => {
if (status == google.maps.DirectionsStatus.OK) {
let routes: IRouteInfo[] = response.routes
.map(route => {
let routeInfo: IRouteInfo = {
summary: route.summary,
distance: route.legs[0].distance.text,
timeValue: route.legs[0].duration.text,
};
return routeInfo;
});
destination.routes = routes;
this.events.publish(AppSettings.UPDATE_DESTINATIONS)
} else if (status == google.maps.DirectionsStatus.OVER_QUERY_LIMIT){
console.warn(status);
} else {
console.warn(status);
}
});
}
/------------code----------------
getDistanceByBlocks(目的地:IDestination[]){
设极限=8;
if(destinations.length){
这个.getDistance(目的地);
});
}否则{
let selection=destinations.slice(0,limit)
让rest=destinations.slice(限制)
selection.forEach(destination=>{
这个.getDistance(目的地);
});
设置超时(()=>{
这个.getDistanceByBlocks(rest)
},3000);
}
}
getDistance(目的地:IDestination){
if(this.dataService.getCurrentLocation()){
让mapSettings=this.userSettings.getMapSettingsSync();
this.mapService.getRoutes(目的地,地图设置);
}
}
//------------------地图服务----------------
getRoutes(目的地:IDestination,地图设置:IMapSettings):无效{
让directionsService=new google.maps.directionsService;
this.currentLocation=this.dataService.getCurrentLocation();
方向服务.路线(
{
原点:{lat:this.currentLocation.latitude,
lng:this.currentLocation.longitude},
目的地:{lat:destination.latitude,lng:destination.longitude},
ProviderRouteAlternations:mapSettings.ProviderRouteAlternations,
avoidHighways:mapSettings.avoidHighways,
AvoidTols:mapSettings.AvoidTols,
驾驶选项:{
出发时间:新日期(Date.now()),
trafficModel:mapSettings.trafficModel
},
travelMode:google.maps.Directions travelMode.DRIVING,
unitSystem:google.maps.unitSystem.IMPERIAL
},
(响应、状态)=>{
if(status==google.maps.directionstatus.OK){
let routes:IRouteInfo[]=response.routes
.map(路线=>{
让routeInfo:IRouteInfo={
概要:route.summary,
距离:route.legs[0]。distance.text,
时间值:route.legs[0]。duration.text,
};
返回路由信息;
});
destination.routes=路线;
this.events.publish(AppSettings.UPDATE\u目的地)
}else if(status==google.maps.directionstatus.OVER\u QUERY\u LIMIT){
控制台。警告(状态);
}否则{
控制台。警告(状态);
}
});
}
需要注意的是,在Chrome网络选项卡中,我们可以看到每次通话之间的等待时间以及我遇到的错误数量。让我很感兴趣的是,我没有看到失败的呼叫,chrome没有向我显示它们。所以我认为google.maps服务是用javascript处理的,它没有调用服务器,但我真的不知道
我在一个项目中遇到了同样的问题。我想你必须在每次请求之间留出一段时间。像这样,服务器持续充电。对于我的项目,我使用以下代码:
for (var i = 0; i<6; i++) {
(function (i) {
setTimeout(function () {
var service = new google.maps.places.PlacesService(map);
var request = {
placeId: yourArray [i]
};
service.getDetails(request, callback);
}, i * 300);
})(i);
}
如果您有任何问题或意见,请告诉我。您的代码中是否使用了Google Maps Javascript API v3密钥?您确定在请求之间有额外的延迟吗?在我看来,你立即发出8个请求,其余的3秒钟后。你是从哪里得到每秒10次查询的查询速率的?嗨,geocodezip,我使用G Maps API键的方式是“在每个块请求之间添加延迟(同时为10次或更少),你可以在图片上看到延迟”Chrome网络示例“这是我分享的。许多谷歌API每秒有10个调用作为限制。而且,当我同时打10个电话时,所有这些都能正常工作,当我尝试打10个以上的电话时,就会出现错误。例如,如果我打11个电话,我将收到10个回复和一个超过查询限制的错误。谢谢你的帮助。我已经尝试过这个选项,在每个请求之间使用延迟,但它不起作用,我会耐心地再试一次,并分享结果。
function callback (place, status) {
if (status == google.maps.places.PlacesServiceStatus.OK) {
...
} else if (status != google.maps.places.PlacesServiceStatus.OK && status != google.maps.places.PlacesServiceStatus.OVER_QUERY_LIMIT) {
console.error("Fail get detail try to rerequest.:(");
...
// For specials errors.
} else if (status == google.maps.places.PlacesServiceStatus.OVER_QUERY_LIMIT) {
console.error("Over Query Limit");
retry ();
}
}