const privateKey = '高德地图key';
const axios = require('axios')
const keywords = '美食|面包'
const adCityCode = '110000'
let allData = [] // 保存每一页数据
serach(1)
function serach(page) {
getOnePageData(page, (res) => {
console.log(page, res)
allData.concat(res.data)
if (res.nexPage) {
page = page + 1;
serach(page) // 递归调用搜索,异步
}
})
}
// 异步获取一页数据
function getOnePageData(page, callBack, pageSize = 20) {
let url = 'https://restapi.amap.com/v3/place/text'
axios.get(url, {
params: {
key: privateKey,
city: adCityCode,
keywords: keywords,
citylimit: true,
offset: pageSize,
page: page
}
}).then(res => {
if (res.data.pois && res.data.pois.length > 0) {
callBack({
data: res.data.pois,
nexPage: true
})
} else {
callBack({
nexPage: false
})
}
})
}
网友评论