[英]How to loop through additional pages using google places and javascript API?
我正在構建一個單頁應用程序,並且正在使用前端javascript API訪問google地圖和地方。 我可以成功生成一個Google地圖並通過此api獲取場所詳細信息,但是,javascript API不會返回“ next_page_token”,這使我可以在前20個之后搜索其他結果。
您如何使用javascript API來獲取next_page_token來遍歷多頁結果? 我很絕望,因為我在任何地方都找不到關於此的任何信息。
編輯-包括一些其他信息。
使用Google在此處提供的文檔進行一次nearSearch ,僅返回一組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)
})
這是谷歌發回的郵件。 從devtools中的控制台獲取。
Results (20) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
附近搜索的結果請求第三個參數。
a4 {f: ƒ, b: ƒ, l: "CqQCIAEAAPcNpixD40jA39VnP-WMOZT21nGCcfpK_smpYhG66W…UWk72OjPB1A7Mdudz6UhoUD_vqC-QSk7CKnoveUXK0IHnQ8es", j: 1528768999850, hasNextPage: true}
從Google頁面搜索API
訪問其他結果:
默認情況下,每個“鄰近搜索”或“文本搜索”每個查詢最多返回20個建立結果。 但是,每次搜索最多可以返回60個結果,分為三頁。 如果您的搜索返回的結果大於20,則搜索響應將包含一個附加值– next_page_token。 將next_page_token的值傳遞給新搜索的pagetoken參數,以查看下一組結果。 如果next_page_token為null或未返回,則沒有進一步的結果。 從發出next_page_token到它生效之間有短暫的延遲。 在下一頁可用之前請求它會返回一個INVALID_REQUEST響應。 使用相同的next_page_token重試請求將返回結果的下一頁
簡而言之,來自以下呼叫的響應: https : //maps.googleapis.com/maps/api/place/textsearch/xml?query=restaurants+ in+ Sydney&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
我正在看文檔頁面的“ 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();
};
});
因為請注意,回調函數使用第三個參數來獲取其他結果。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.