【发布时间】:2018-04-18 23:09:28
【问题描述】:
我正在使用 Google Maps API 显示 20 个地点的列表以及到达那里的时间。我正在使用 google.maps.DirectionsService。这个 API 的每秒限制设置为 10 次调用,如果我只有 10 个位置,我可以同时调用所有位置(经过测试,效果很好)。当我有超过 10 个位置时,我会使用呼叫块进行呼叫。例如有 16 个位置:我调用前 10 个位置,然后等待 3 秒,然后调用其他 6 个位置。
理论上,这种方法应该可行,因为我遵守每秒 10 次的限制。 但是,不起作用。当我尝试调用其余 6 个位置时,我收到错误 OVER_QUERY_LIMIT,有时其中 4 个失败,有时其中 3 个失败。如果我将等待时间增加到 5 秒,有时可以正常工作,有时其中一个会失败。
问题:这个限制不应该是每秒 10 次查询吗?如果是,那么我做错了什么?以及如何解决?
//------------------ 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);
}
});
}
请务必注意,在 Chrome 网络标签中,我们可以看到每次通话之间的等待时间以及我遇到的错误数量。让我很感兴趣的是我没有看到失败的调用,chrome 没有向我显示它们。所以我认为 google.maps 服务正在用 javascript 处理它,它没有调用服务器,但我真的不知道。
【问题讨论】:
-
您是否在代码中使用了 Google Maps Javascript API v3 密钥?
-
您确定请求之间有额外的延迟吗?在我看来,您立即发出 8 个请求,其余 3 秒后发出。你从哪里得到每秒 10 次查询的查询率?
-
嗨 geocodezip,我在 index.html 中以这种方式使用 G Maps API 密钥“
-
每个block请求之间添加延迟(同时10个或更少),你可以在我分享的图片“Chrome网络示例”上看到延迟。
-
许多 Google API 以每秒 10 次调用为限制。此外,当我同时拨打 10 个电话时,它们都运行良好,当我尝试拨打 10 个以上电话时,错误就会出现。例如,如果我拨打 11 次电话,我将收到 10 次回复和 OVER_QUERY_LIMIT 错误。
标签: javascript google-chrome google-maps-api-3