1. 程式人生 > 程式設計 >小程式如何定位所在城市及發起周邊搜尋

小程式如何定位所在城市及發起周邊搜尋

request封裝

為小程式get/post的promise封裝

rq.js

/*
 * url {String} 請求地址介面 
 * data {Object} 請求引數 
 * param {Object} request引數
 * method {String} 指定使用post或者是get方法
*/
export function request(url,data={},param={},method='POST') {
 return new Promise((resolve,reject)=>{
  let postParam = {
   url,method,data,// timeout
   // dataType
   // responseType
   header: {
    'content-type': 'application/json' // 預設值
   },success(res) {
    resolve(res)
   },error: function (e) {
    reject(e)
   }
  }
  postParam = Object.assign(postParam,param)
  postParam.fail = postParam.error
  if (postParam.url) wx.request(postParam)
 })
}

module.exports = {
 get(url,param){
  return request(url,method='GET')
 },post(){
  return request.apply(null,arguments)
 }
}

位置服務方法

需要開通小程式的位置服務功能,在小程式控制臺

小程式如何定位所在城市及發起周邊搜尋

簡單的封裝了三個位置服務的方法

  • 所在地城市
  • 地區搜尋
  • 範圍搜尋
// request的promise封裝
const iKit = request('./rq.js') 

// key為開通位置服務所獲取的查詢值
// 下面這個key是隨便寫的
let key = 'JKDBZ-XZVLW-IAQR8-OROZ1-XR3G9-UYBD5'

/*
 * 搜尋地區... 
 * 搜尋地區的商圈, 如搜尋 kfc 廣州市
 * key {String} 搜尋內容 
 * region {String} 搜尋區域 
*/
export function searchRegion(kw,region) {
 let opts = {
  keyword: encodeURI(kw),boundary: region ? `region(${encodeURI(region)},0)` : '',// 0 為限定範圍搜搜索,1為開放範圍搜素偶
  page_size: 10,// 搜尋結果分頁最大條數
  page_index: 1,// 指定分頁
  key
 }

 return new Promise((resolve,rej)=>{
  iKit.get('https://apis.map.qq.com/ws/place/v1/search',opts).then(res=>{
   resolve(res.data.data)
  })
 })
}

/*
 * 搜尋附近的... 
 * 以當前位置的經緯度搜索附近商圈,如附近的酒店,快餐等等
 * key {String} 搜尋內容 
 * params {Object} 搜尋引數 包含三個引數 lat緯度,lng經度,distance範圍(單位米) 
*/
export function searchCircle(kw,params={}) {
 let {lat,lng,distance} = params
 if (!lat && !lng) return 
 if (!distance) distance = 1000 // 搜尋範圍預設1000米
 let opts = {
  keyword: encodeURI(kw),boundary: `nearby(${lat},${lng},${distance})`,orderby: '_distance',// 範圍搜尋支援排序,由近及遠 
  page_size: 20,// 搜尋結果分頁最大條數 
  page_index: 1,// 指定分頁 
  key
 }

 return new Promise((resolve,opts).then(res=>{
   resolve(res.data.data)
  })
 })
}

// 所在地的城市,省份等區域資訊
/*
 * 所在地的城市,省份等區域資訊 
 * 如當前地址所在省份、城市 
 * lat {Number} 緯度
 * lng {Number} 經度 
*/
export function myCity(lat,lng) {
 return new Promise((resolve,rej)=>{
  let opts = {
   location: `${lat},${lng}`,key
  }
  
  iKit.get(`https://apis.map.qq.com/ws/geocoder/v1/`,opts).then(res => {
   resolve(res.data.result)
  })
 })
}

呼叫

wx.getLocation({
  type: 'wgs84',success(location) {
   locationPosition = location
   
   // 所在地資訊
   myCity(location.latitude,location.longitude).then(res => {
    let myCityInfo = res.ad_info
    let {city,nation,province,city_code,adcode} = myCityInfo
    console.log({title: `國家: ${nation},省份: ${province},城市: ${city}`})
   })
   
   // 附近搜尋
   searchCircle('快餐',{
    lat: location.latitude,lng: location.longitude,distance: 500
   }).then(res=>{
    console.log(res)
   })
   
   // 地區搜尋
   searchRegion('酒店','廣州').then(res=>{
    console.log(res)
   })
  }
})

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。