【问题标题】:Google Maps API - OVER QUERY LIMIT per second limitGoogle Maps API - OVER QUERY LIMIT 每秒限制
【发布时间】: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 处理它,它没有调用服务器,但我真的不知道。

Chrome Network Tab Example

【问题讨论】:

  • 您是否在代码中使用了 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


【解决方案1】:

我在我的一个项目中遇到了同样的问题。我认为您必须在每个请求之间留出时间。像这样,服务器不断收费。对于我的项目,我使用以下代码:

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

在这里,我定义请求并发送它。请注意,我在每个请求之间设置了 0.3 秒的时间。

您还可以使用此代码来了解何时出现 OVER_QUERY_LIMIT 错误:

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

}

如果您有任何问题或一些 cmets,请告诉我。

【讨论】:

  • 感谢您的帮助。我已经尝试过此选项,在每个请求之间使用延迟,但它不起作用,我会更加耐心地再试一次,我会分享结果。
猜你喜欢
  • 2012-12-10
  • 1970-01-01
  • 1970-01-01
  • 2012-10-17
  • 1970-01-01
  • 2018-11-09
  • 2017-08-03
  • 2018-06-27
相关资源
最近更新 更多