如何使用 google 个位置和 javascript API 循环浏览其他页面?

How to loop through additional pages using google places and javascript API?

我正在构建单页应用程序并使用前端 javascript API 访问 google 地图和地点。我成功地生成了一个 google 地图并通过这个 api 获取了企业详细信息,但是 javascript API 没有 return 一个 "next_page_token" 允许我搜索前 20 个之后的其他结果。

如何使用 javascript API 让 next_page_token 循环遍历多页结果?我很绝望,因为我在任何地方都找不到关于这个的任何信息。

编辑 - 包括一些附加信息。

使用文档 provided by Google here 进行 nearbySearch 只是 return 一组 20 个结果,响应中没有其他属性。

这是我使用 Javascript API 发出的请求。我的 index.html 文件中包含脚本。

let map = new google.maps.Map(this.$refs.map, {
    center: this.location,
    zoom: 12,
    disableDefaultUI: true,
    scrollwheel: false
})

let request = {
    location: this.location,
  radius: 10000,
  type: ['restaurants'] 
}

let service = new google.maps.places.PlacesService(map)
service.nearbySearch(request, (results, status) => {
    console.log("Results", results)
    console.log("Status", status)
})

这是 google 发回的内容。从 devtools 中的控制台获取。

Results (20) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]

nearbySearch 的结果请求第三个参数。

a4 {f: ƒ, b: ƒ, l: "CqQCIAEAAPcNpixD40jA39VnP-WMOZT21nGCcfpK_smpYhG66W…UWk72OjPB1A7Mdudz6UhoUD_vqC-QSk7CKnoveUXK0IHnQ8es", j: 1528768999850, hasNextPage: true}

我正在查看 the doc page for "nearbySearch",这个例子很突出:

// Perform a nearby search.
  service.nearbySearch(
      {location: pyrmont, radius: 500, type: ['store']},
      function(results, status, pagination) {
        if (status !== 'OK') return;

        createMarkers(results);
        moreButton.disabled = !pagination.hasNextPage;
        getNextPage = pagination.hasNextPage && function() {
          pagination.nextPage();
        };
      });

因为注意回调采用第三个参数,用于获取其他结果。

从google页面搜索API

访问其他结果:

By default, each Nearby Search or Text Search returns up to 20 establishment results per query; however, each search can return as many as 60 results, split across three pages. If your search will return more than 20, then the search response will include an additional value — next_page_token. Pass the value of the next_page_token to the pagetoken parameter of a new search to see the next set of results. If the next_page_token is null, or is not returned, then there are no further results. There is a short delay between when a next_page_token is issued, and when it will become valid. Requesting the next page before it is available will return an INVALID_REQUEST response. Retrying the request with the same next_page_token will return the next page of results

简而言之,在电话回复中,例如: https://maps.googleapis.com/maps/api/place/textsearch/xml?query=restaurants+in+Sydney&key=YOUR_API_KEY

-或-

https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-33.8670522,151.1957362&rankby=distance&type=food&key=YOUR_API_KEY

你会发现:

{
   "html_attributions" : [],
   "next_page_token" : "CpQCAgEAAFxg8o-eU7_uKn7Yqjana-HQIx1hr5BrT4zBaEko29ANsXtp9mrqN0yrKWhf-y2PUpHRLQb1GT-mtxNcXou8TwkXhi1Jbk-ReY7oulyuvKSQrw1lgJElggGlo0d6indiH1U-tDwquw4tU_UXoQ_sj8OBo8XBUuWjuuFShqmLMP-0W59Vr6CaXdLrF8M3wFR4dUUhSf5UC4QCLaOMVP92lyh0OdtF_m_9Dt7lz-Wniod9zDrHeDsz_by570K3jL1VuDKTl_U1cJ0mzz_zDHGfOUf7VU1kVIs1WnM9SGvnm8YZURLTtMLMWx8-doGUE56Af_VfKjGDYW361OOIj9GmkyCFtaoCmTMIr5kgyeUSnB-IEhDlzujVrV6O9Mt7N4DagR6RGhT3g1viYLS4kO5YindU6dm3GIof1Q",
   "results" : [{...}]
}

重要说明:如果没有其他结果可显示,则不会返回 next_page_token。最多可返回 60 个结果。发出 next_page_token 和它生效之间有短暂的延迟。

阅读更多: https://developers.google.com/places/web-service/search#PlaceSearchPaging