You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
86 lines
3.3 KiB
86 lines
3.3 KiB
const md5 = require('./md5.min.js')
|
|
const key = 'BDFBZ-NXGWR-EUSWI-WGQGL-V7GSK-VSBDJ'
|
|
const sk = 'slBA3zFw9fTSjEbZKJ9hGH6DinYFqYFS'
|
|
|
|
function request(params, url){
|
|
let keys = Object.keys(params).sort()
|
|
let query = []
|
|
keys.forEach(key => {
|
|
query.push(key + '=' + params[key])
|
|
})
|
|
query = url + query.join('&')
|
|
let sign = md5(query + sk)
|
|
return new Promise(function (resolve, reject) {
|
|
wx.request({
|
|
url: `https://apis.map.qq.com${query}&sig=${sign}`,
|
|
success: res => {
|
|
if(res.statusCode == 200 && res.data && res.data.status == 0){
|
|
resolve(res.data)
|
|
} else {
|
|
reject('数据错误')
|
|
}
|
|
},
|
|
fail: err => {
|
|
reject('网络异常')
|
|
}
|
|
})
|
|
})
|
|
}
|
|
|
|
function geocoder(latitude, longitude){
|
|
let params = { location: `${latitude},${longitude}`, key }
|
|
return request(params, '/ws/geocoder/v1?')
|
|
}
|
|
|
|
/**
|
|
* @param {*} keyword 用户输入的关键词(希望获取后续提示的关键词)必填
|
|
* @param {*} region 限制城市范围:根据城市名称限制地域范围, 如,仅获取“广州市”范围内的提示内容
|
|
* @param {*} latitude 定位坐标,传入后,若用户搜索关键词为类别词(如酒店、餐馆时),与此坐标距离近的地点将靠前显示,格式: location=lat,lng
|
|
* @param {*} longitude 定位坐标,传入后,若用户搜索关键词为类别词(如酒店、餐馆时),与此坐标距离近的地点将靠前显示,格式: location=lat,lng
|
|
*/
|
|
function suggestion(keyword, region, location){
|
|
let params = { keyword, region, location: `${location.latitude},${location.longitude}`, region_fix: 1, page_size: 20, get_subpois: 0, key }
|
|
return request(params, '/ws/place/v1/suggestion?')
|
|
}
|
|
|
|
//https://apis.map.qq.com/ws/place/v1/explore
|
|
function explore(location, radius, num, size){
|
|
let params = { boundary: `nearby(${location.latitude},${location.longitude},${radius})`, page_index: num, page_size: size, key }
|
|
return request(params, '/ws/place/v1/explore?')
|
|
}
|
|
//骑行(bicycling)路线规划
|
|
function bicycling(from, to){
|
|
let params = { from: `${from.latitude},${from.longitude}`, to: `${to.latitude},${to.longitude}`, key }
|
|
return request(params, '/ws/direction/v1/bicycling?')
|
|
}
|
|
// 步行(walking)路线规划
|
|
function walking(from, to){
|
|
let params = { from: `${from.latitude},${from.longitude}`, to: `${to.latitude},${to.longitude}`, key }
|
|
return request(params, '/ws/direction/v1/walking?')
|
|
}
|
|
// 驾车(driving)路线规划;waypoints:途经点,格式:lat1,lng1;lat2,lng2;… 最大支持16个>>waypoints=39.951004,116.571980
|
|
function driving(from, to, waypoints){
|
|
let params = { from: `${from.latitude},${from.longitude}`, to: `${to.latitude},${to.longitude}`, key }
|
|
if(waypoints && Array.isArray(waypoints)){
|
|
var points = ''
|
|
for (let index = 0; index < waypoints.length; index++) {
|
|
if(index == 0){
|
|
points += waypoints[index].latitude + ',' + waypoints[index].longitude
|
|
} else {
|
|
points += ';' + waypoints[index].latitude + ',' + waypoints[index].longitude
|
|
}
|
|
params.waypoints = points
|
|
}
|
|
}
|
|
return request(params, '/ws/direction/v1/driving?')
|
|
}
|
|
|
|
module.exports = {
|
|
key: key,
|
|
geocoder: geocoder,
|
|
suggestion: suggestion,
|
|
explore: explore,
|
|
bicycling: bicycling,
|
|
walking: walking,
|
|
driving: driving
|
|
}
|