美文网首页
微信小程序——根据定位地点搜索附近超市

微信小程序——根据定位地点搜索附近超市

作者: 无题syl | 来源:发表于2021-05-06 17:41 被阅读0次

一、需求
自己在写购物商城小程序时,有根据当前位置选择附件自提点超市
可以看到具体距离远近。
二、关键调用接口
腾讯位置服务里的开发文档->WebService API 有相关接口

000.JPG 8989.JPG

https://apis.map.qq.com/ws/place/v1/search
调用此接口获取信息

三、接口参数
boundary:需要找到当前位置的经纬度信息,小程序接口可以得到,调用wx.chooseLocation当选择好位置后,可以得到位置的经纬度。
key:腾讯文档中可以找到申请入口,直接申请即可。
其他参数看文档

四、代码

//封装经度纬度
wx.$getLatLong = () => {
  return new Promise((resolve, reject) => {
    wx.chooseLocation({
      success: ({
        latitude,
        longitude,
        address,
        name
      }) => {
        let from = latitude + ',' + longitude
        //console.log(from)
        resolve({
          from,
          address,
          name
        })
      }
    })
  })
}
async getztd(){
  let {from,address,name}=await wx.$getLatLong()
  //console.log(from,address,name)
  this.setData({
    address:address+name
  })
  wx.setStorage({
    data: this.data.address,
    key: 'ztd',
  })
  wx.setStorage({
    data: from,
    key: 'from',
  })

  //console.log(from)
  let res=await wx.$get('https://apis.map.qq.com/ws/place/v1/search',{
    keyword:encodeURI('超市'),
    boundary:`nearby(${from},1000)`,
    page_size:5,
    page_index:1,
    key:'HNVBZ-A5FCF-62KJV-NUOYU-E4HC6-T7FB5'
  })
  
  //console.log(res)
  this.setData({
    ztdList:res.data
  })
},

相关文章

网友评论

      本文标题:微信小程序——根据定位地点搜索附近超市

      本文链接:https://www.haomeiwen.com/subject/pepddltx.html