使用 javascript places 库无法通过 nearbySearch 或 textSearch 返回 "next_page_token"

Not getting "next_page_token" back with nearbySearch or textSearch using javascript places library

我试图通过使用 nearbySearch 或 textSearch 遍历结果 google returns 来捕获特定城市的所有餐馆。到目前为止,我只能得到前 20 个结果。我期待在响应中看到一个名为 "next_page_token" 的参数,我可以将其与下一个请求一起发送以获得下一组结果,但是,无论我点击哪个 API 或半径大小,我从未在 google.

的回复中取回 "next_page_token"

我已经尝试在我的回调函数中专门请求 "pagetoken" 作为回调参数,令我惊讶的是它确实为我提供了 pagetoken 详细信息。我尝试在我的连续请求中将 pagetoken.l 映射到 pagetoken,但它仍然是 returns 相同的结果集。

我已经在线搜索了几个小时,但无法解决这个问题。

使用Node和VueJS做客户端请求。下面的示例代码位于脚本部分的 VueJS 模板之一中。 textSearch 几乎相同,只是我将位置和半径替换为使用关键字执行搜索的查询。

使用 nearbySearch 的示例

initialize(addressData, placeResults) {
    console.log("Address Data", addressData)
    console.log("Place Results", placeResults)

    this.location = {
        lat: addressData.latitude,
        lng: addressData.longitude
    }

    this.initMap()

},
initMap() {
    console.log("Refs", this.$refs)
    console.log("location", this.location)

    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: ['restaurant']      
    }

    let service = new google.maps.places.PlacesService(map)
    service.nearbySearch(request, (results, status, pagetoken) => {
        console.log("Results", results)
        console.log("Status", status)
        console.log("Pagetoken", pagetoken)
        if (pagetoken.hasNextPage) {
            this.hasNextPage = true
            this.pagetoken = pagetoken.l
        }
    })
}

我不会 post 结果,因为 JSON 很长,但结果只包含找到的一系列餐馆,没有其他内容。

来自回调的 Pagetoken 响应

{
  "l": "CqQCHwEAAHGTZMjzfdC-1RcLP8jYBOGy7oUFztGWLSWOCQGAcHe4NgVLfJRl1Yl1GH0G-mju4-A-1fwtDChtmoSQn_qNBIkiK8co2KWy28M3BhV95yvLAswbfTIRwB9wZAev2aaRc3Yd2_nY9dWbFMhv-KppnTGKJj7dYndtj-yyUwV2evznSAE_t5Fcd7OYHmBXPHjSL54jfadw2B96wEx_Ju-5O30J14LlXLkJQZHhCNaD-kb-OAofXVeY5My1-LavHDgdkw49vTeiMou7jU4wB7m47ZSWAB2NChYt5tHLeHPA8n7aCRc40cEzNwpI8T3nwrw8tsuBx9Lbh8mToaUbULBSTzKQzr3DC6v2N4AGDuK4v6A_AdzKZVfhD_GNcM1olyQ2xRIQ0sAvkEOTSJ4ByvjcRXs_uhoUm0CPRm7lHLG593au02QlXTEjeg4",
  "j": 1528657652795,
  "hasNextPage": true
}

根据文档,附近搜索的回调函数中的第三个参数是PlaceSearchPagination类型的对象:

https://developers.google.com/maps/documentation/javascript/reference/3/places-service#PlaceSearchPagination

PlaceSearchPagination 公开布尔类型的 public 属性 hasNextPage 和 public 方法 nextPage() 以获取下一页结果。

nextPage() - Fetches the next page of results. Uses the same callback function that was provided to the first search request.

我相信您应该使用以下代码在可用时获取下一页结果

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