在制作收货地址时,通常会要求用户填写或打开地图选择收货地址。这时候就需要用到微信提供的地址API。使用地址API时,需要注册相应的API,地址API会经常调整,需要关注官方公告,下面介绍地址API的使用。在第一部分中,首先检查是否启用了此设置。进入微信开发者文档,在API下找到location,这里是位置信息API。在使用位置信息之前,需要在app.json中注册位置信息api。这里我使用的是wx.chooseLocation,所以你可以在app.json中注册这个api。注册完成后,在事件中使用//选择页面地址调用该接口。wx.chooseLocation({latitude:0,success(res){console.log(res);//选择的地址信息}})},会弹出API的第二部分,需要在app中声明权限字段.json。虽然你已经在requiredPrivateInfos注册了,但是在使用API??之前还是需要在permission中注册。只是//获取当前地址信息wx.getLocation({type:'gcj02',//返回可用于wx.openLocation的经纬度success(res){constlatitude=res.latitude//维度constlongitude=res.longitude//经度wx.openLocation({latitude,longitude,scale:18})}})打开位置信息,自动定位到当前位置//地址addAddress(){letthat=this//选择地址wx.getLocation({//获取当前地址信息,地理位置,速度type:'gcj02',//返回即可Latitudeandlongitudeforwx.openLocationsuccess(res){constlatitude=res.latitude//维度constlongitude=res.longitude//经度wx.chooseLocation({//通过经纬度自动定位当前位置latitude,//dimensionlongitude,//经度success(res){wx.setStorageSync('address',res.address+res.name)that.setData({address:res.address+res.name//保存当前位置信息,echo})}})}})},
