0

I am trying to use a nearby search with the Google Places API and I am trying to get the next page of results. I understand the next page token doesn't become valid for a couple seconds after your previous request. However, the token NEVER returns results with the 'OK' status response.

How do I get a consistent, "OK" status response from the nearby search request?

  const location = {latitude: 37.822919, longitude: -122.000685};
  const place_types = ["premise", "establishment"];
  //Get initial response (which works) and includes next page token if > 20 results
  const response = await fetch(
    'https://maps.googleapis.com/maps/api/place/nearbysearch/json? location=' +
     location.latitude + ',' + location.longitude + '&radius=150' +
    '&types=' + place_types.toString() + '&key=' + API_KEY);
  const responseJson = await response.json();
  let status, responseJson2;
  let now = new Date();

  do {
    await new Promise(resolve => {
      setTimeout(resolve, 100);
    });

    const response2 = await fetch(
      'https://maps.googleapis.com/maps/api/place/nearbysearch/json?' +
      'key=' + API_KEY + '&pagetoken=' + next_page_token,
    );
    responseJson2 = await response2.json();

    status = responseJson2.status;
    console.log(status)

    if (new Date() - now > 10000) {
      console.log('[ERROR] Timeout waiting for valid next page token.');
      break;
    }
  } while (status === 'INVALID_REQUEST');

current output: [ERROR] Timeout waiting for valid next page token.

As you can see I set a timeout condition to break the loop if it takes longer than 30 seconds. However, I have tested the code without that and let it wait for an 'OK' response for minutes without success.

Also note that I didn't include the initialization of next_page_token variable, but it does indeed have the next page token as its value.

matthias_h
  • 11,356
  • 9
  • 22
  • 40
Dane B
  • 169
  • 12
  • 1
    If the request is being made from frontend JavaScript code running in a browser, what errors is the browser logging in the devtools console? – sideshowbarker Apr 03 '20 at 22:53
  • 1
    Can you provide a [mcve] that shows how you make the original request and how the `next_page_token` is initialized? – geocodezip Apr 04 '20 at 00:12
  • Hi I would like for this question to be considered to be reopened. I have included more detail about the desired behavior (How to get a consistent 'OK' response) as well as edited my code to provide a minimal reproducible example. I have also included my current output. Thanks. – Dane B Apr 04 '20 at 00:46
  • 2
    Where is the **required** delay between receiving the `next_page_token` and using it? Possible duplicate of: [Paging on Google Places API returns status INVALID_REQUEST](https://stackoverflow.com/questions/21265756/paging-on-google-places-api-returns-status-invalid-request) – geocodezip Apr 04 '20 at 01:08

1 Answers1

0

Add a request count to the end of each new request so as to avoid a cached response from Google:

  const location = {latitude: 37.822919, longitude: -122.000685};
  const place_types = ["premise", "establishment"];
  //Get initial response (which works) and includes next page token if > 20 results
  const response = await fetch(
    'https://maps.googleapis.com/maps/api/place/nearbysearch/json? location=' +
     location.latitude + ',' + location.longitude + '&radius=150' +
    '&types=' + place_types.toString() + '&key=' + API_KEY);
  const responseJson = await response.json();
  let status, responseJson2;
  let now = new Date();
  let request_count = 0;

  do {
    await new Promise(resolve => {
      setTimeout(resolve, 100);
    });

    const response2 = await fetch(
      'https://maps.googleapis.com/maps/api/place/nearbysearch/json?' +
      'key=' + API_KEY + '&pagetoken=' + next_page_token + '&request_count=' + request_count,
    );
    responseJson2 = await response2.json();

    status = responseJson2.status;
    console.log(status)

    if (new Date() - now > 10000) {
      console.log('[ERROR] Timeout waiting for valid next page token.');
      break;
    }
    request_count++;
  } while (status === 'INVALID_REQUEST');
Dane B
  • 169
  • 12