带有新API密钥的Google Places Library OVER_QUERY_LMIT

Google Places Library OVER_QUERY_LIMIT with new API key

本文关键字:OVER Library QUERY LMIT Places Google API 密钥      更新时间:2023-09-26

我正在javascript应用程序中使用Places库。当我使用service.nearbySearch()方法查找附近的地方时,它工作得很好。当我发出service.getDetails()请求时,我会得到每个请求的OVER_QUERY_LIMIT状态。在我的开发人员控制台中,我可以跟踪对Google Maps JavaScript API v3的每个请求,但我不会从API地方得到任何结果。

以下是一些代码:

// How I load the library 
<script src="https://maps.googleapis.com/maps/api/js?libraries=places&key=API_KEY"></script>
// My places request
var places = new google.maps.places.PlacesService(map);
var location = new google.maps.LatLng(lat, lng);
var request = {
    location: location,
    radius: radius,
    types: [type]
  };
places.nearbySearch(request, function(results, status, pagination) {
    if (status === google.maps.places.PlacesServiceStatus.OK) {
      for (var i = 0; i < results.length; i++) {
      // Get the details for each place
      var detailsRequest = { placeId: results[i].id }
      places.getDetails(detailsRequest, function(place, status) {
        if (status == google.maps.places.PlacesServiceStatus.OK) {
            console.log('PLACE', place)
        } else {
            console.log('STATUS', status)
        }        
    })              
  };
}

有什么想法吗?

答案是我请求.getDetails()方法的速度太快。文档。

我迭代了几十个位置,请求太多太快。通常情况下,在OVER_QUERY_LIMIT错误之前,我只会得到60+个结果中的前10个或12个。

我将对.getDetails()的调用移动到标记的单击事件。这样一次只发送一个请求。

也遇到了同样的问题。似乎你有一个9或10个请求池,你可以耗尽,然后每1秒就可以获得一个额外请求的余量。

有人说API的服务器端版本每秒允许50个请求,所以我猜谷歌正在努力防止客户端实例过载,这是有道理的。

对我来说,当getDetails结果/s出现时,只需要UI显示微调器就足够了。所以我只是在前9或10个请求之后抑制了请求,就像这样:

nearbySearchCallback: function (places, status) {
  var that = this;
  if (status === window.google.maps.places.PlacesServiceStatus.OK) {
    for (var i = 0; i < places.length; i++) {
      // You seem to have a pool of 9 or 10 requests to exhaust,
      // then you get another request every second therein. 
      (function (i) {
        setTimeout(function () {
          service.getDetails({ placeId: places[i].place_id }, that.getDetailsCallback);
        }, i < 9 ? 0 : 1000 * i);
      })(i);
    }
  }
},
getDetailsCallback: function (place, status) {  /* ... */ }