【问题标题】:Google Places API next_page_token never returns 'OK' response for nearbySearchGoogle Places API next_page_token 从不返回附近搜索的“OK”响应
【发布时间】:2020-04-03 22:48:05
【问题描述】:

我正在尝试通过 Google Places API 使用附近搜索,并且正在尝试获取下一页结果。我了解下一页令牌在您上次请求后的几秒钟内无效。但是,令牌永远不会返回带有“OK”状态响应的结果。

如何从附近的搜索请求中获得一致的“OK”状态响应?

  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');

当前输出:[ERROR] Timeout waiting for valid next page token.

如您所见,如果循环时间超过 30 秒,我设置了一个超时条件来中断循环。但是,我已经测试了没有它的代码,让它等待几分钟的“OK”响应,但没有成功。

另外请注意,我没有包含 next_page_token 变量的初始化,但它确实将下一页标记作为其值。

【问题讨论】:

  • 如果请求是从浏览器中运行的前端 JavaScript 代码发出的,浏览器在 devtools 控制台中记录的错误是什么?
  • 您能否提供一个minimal reproducible example,说明您如何提出原始请求以及如何初始化next_page_token
  • 您好,我希望重新考虑这个问题。我已经包含了有关所需行为的更多详细信息(如何获得一致的“OK”响应),并编辑了我的代码以提供一个最小的可重现示例。我还包括了我当前的输出。谢谢。
  • 收到next_page_token 和使用它之间的必需延迟在哪里?可能重复:Paging on Google Places API returns status INVALID_REQUEST

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


【解决方案1】:

在每个新请求的末尾添加一个请求计数,以避免来自 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');

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多