在做收货地址的时候,通常会让用户填写或者关上地图抉择播种地址,此时就须要用到微信提供的地址API,在应用地址API的时候须要注册对应的API,而且地址API会常常调整,须要关注官网布告,上面就是对于地址API的应用。
第一局部
先看下此设置有没有关上
进入微信开发者文档,在API下找到地位,此处就是地位信息API了
在应用地位信息之前,须要在app.json中注册地位信息api
此处我应用的是wx.chooseLocation,所以在app.json中注册这个api即可
注册实现后,在页面应用
// 抉择地址 在事件内调用这个api即可 wx.chooseLocation({ latitude: 0, success(res){ console.log(res); // 抉择的地址信息 } }) },
第二局部
局部API会弹出须要在app.json中申明permission字段
尽管在requiredPrivateInfos注册过
然而还须要在permission中注册
此时再应用API即可
// 获取以后地址信息 wx.getLocation({ type: 'gcj02', //返回能够用于 wx.openLocation 的经纬度 success (res) { const latitude = res.latitude // 维度 const longitude = res.longitude // 经度 wx.openLocation({ latitude, longitude, scale: 18 }) } })
关上地位信息,主动定位到以后地位
// 地址 addAddress(){ let that = this // 抉择地址 wx.getLocation({ // 获取以后地址信息,地理位置、速度 type: 'gcj02', //返回能够用于 wx.openLocation 的经纬度 success (res) { const latitude = res.latitude // 维度 const longitude = res.longitude // 经度 wx.chooseLocation({ // 通过经纬度主动定位到以后地位 latitude, // 维度 longitude, // 经度 success(res){ wx.setStorageSync('address', res.address+res.name) that.setData({ address : res.address+res.name // 将以后地位信息保留,回显 }) } }) } }) },