Google Places Library

So I’m trying to log all the places around me, and then grab the reviews of those places with google places library.
I manage to grab all the id’s of places (around 20 of them) and when I try to grab the review, I only get reviews for 9 places and it completely ignores all the other places. Why is that?
Here’s a bit of the code:

var allplaces = [];
var placeID;
var newMap = new google.maps.places.PlacesService(map);

function displayReviews(results, status) {
  if (status === google.maps.places.PlacesServiceStatus.OK) {
    var place;

    for (var i = 0; i < results.length; i++) {
      var place = results[i].place_id;
    for (var i = 0; i < allplaces.length; i++) {
      placeID = { placeId: allplaces[i] };
      newMap.getDetails(placeID, function(allplaces) {

So I have an array (allplaces) which stores all the places id, and I’m running a loop in which I have an object (placeID) and its property of “placeId”. After that, I’m grabbing the details from the new map that was defined outside of this function that should console log allplaces reviews, but I only get a few.

And then I get this error. It’s like the function itself doesn’t follow the order that it was written in. Am I missing something?

Well #1: Make sure you specify what fields you’re going for, or you’ll be billed for retrieving ALL the fields. (This was pointed out on the API.)
#2: Chances are, Google’s API is throttling your requests per second. Instead of getting ALL the reviews at once, only request them when you need them (IE: When a user clicks on the place name.)
If you dont have the review information when they click, retrieve the review info for that specific place, save it (in case they click away and then click back), and then display it.

Something along the lines of…(pseudocode to follow)

on click on ID #2919:
  if reviews[2919] exists:
    display reviews for #2919
     request reviews from google.
     on return, set reviews[2919] to the return value.
     then display reviews for #2919.
1 Like

Thanks a lot! The point of this is to ultimatley display the reviews when a user clicks a place, but I didn’t thought of trying that right away instead of using console. Thanks!

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.