Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Places Library OVER_QUERY_LIMIT with new API key

I'm using the Places libary in my javascript application. It works fine when I use the service.nearbySearch() method to look up nearby places. When I then go to make a service.getDetails() request I get an OVER_QUERY_LIMIT status for each request. In my Developer Console, I can track every request made to the Google Maps JavaScript API v3, but I dont get any results from the places API.

Heres some of the code:

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

Any ideas?

like image 841
JDillon522 Avatar asked Mar 24 '15 12:03

JDillon522


2 Answers

The answer was that I was requesting the .getDetails() method too fast. Docs.

I was iterating over dozens of locations and requesting too many too fast. Often times I would only get the first 10 or 12 our of 60+ results before the OVER_QUERY_LIMIT error.

I moved the call to .getDetails() to the click event for the marker. That way only one request is sent at a time.

like image 139
JDillon522 Avatar answered Sep 29 '22 20:09

JDillon522


Also ran into the same issue. Seems like you have a pool of 9 or 10 requests you can exhaust, and then get allowance for an additional request every 1s.

Some people have said that the server-side version of the API allows for 50 requests per second, so I'm guessing Google is trying to prevent client-side instances overloading them, which makes sense.

For me, it was enough to just have the UI show spinners while the getDetails result/s come in. So I just throttled the requests after the first 9 or 10 like so:

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) {  /* ... */ }
like image 32
benmccallum Avatar answered Sep 29 '22 18:09

benmccallum