带有新API密钥的Google地方信息库OVER_QUERY_LIMIT

时间:2015-03-24 12:17:36

标签: javascript google-maps google-places-api

我在我的javascript应用程序中使用Places libary。当我使用service.nearbySearch()方法查找附近的地方时,它工作正常。当我转到service.getDetails()请求时,我会为每个请求获得OVER_QUERY_LIMIT状态。在我的开发者控制台中,我可以跟踪对Google Maps JavaScript API v3发出的每个请求,但我没有从places 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)
        }        
    })              
  };
}

有什么想法吗?

2 个答案:

答案 0 :(得分:6)

答案是我要求.getDetails()方法过快。 Docs

我在几十个地点进行迭代,请求太多太多。通常情况下,我只会在OVER_QUERY_LIMIT错误之前获得60+结果的前10或12个。

我将电话号码.getDetails()移至了标记的点击事件。这样,一次只发送一个请求。

答案 1 :(得分:1)

也遇到了同样的问题。好像你有一个9或10个请求可以用尽,然后每1秒获得额外请求的补贴。

有些人说API的服务器端版本允许每秒50个请求,因此我猜测Google正试图阻止客户端实例超载它们,这是有道理的。

对我来说,当getDetails结果进入时,只需让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) {  /* ... */ }