一、查看用户授权设置信息
调用wx.getSetting(object),object中有三个可选参数
object{
success(res){
//res对象包含用户启用了那些授权
console.log("res:",res)
},
fail(){
// 失败时调用的回调函数
},
complete(){
//不管成功还是失败都会调用的回调
}
}
二、获取用户当前位置经纬度并调用接口转为地址
调用wx.authorize(),
代码片段:
myAderss(){
let that=this;
wx.authorize({
scope: 'scope.userLocation',
success() {
wx.getLocation({
type: 'wgs84',
success(res) {
var latitude = res.latitude;
var longitude = res.longitude;
console.log("纬度=" + latitude + " 经度=" + longitude);
// 构建请求地址
var qqMapApi = 'http://apis.map.qq.com/ws/geocoder/v1/' + "? location=" + latitude + ',' +
longitude + "&key=" + 'XVLBZ-BSU66-ULJSQ-MFGXD-TM7GZ-55F2M' + "& get_poi=1";
that.sendRequest(qqMapApi)
}
})
}
})
},
//调用qq地图接口,将经纬度转换为街道位置信息
sendRequest: function (qqMapApi) {
let that = this;
// 调用请求
wx.request({
url: qqMapApi,
data: {},
method: 'GET',
success: (res) => {
console.log(res)
if (res.statusCode == 200 && res.data.status == 0) {
// 从返回值中提取需要的业务地理信息数据
that.setData({ nation: res.data.result.address_component.nation });
that.setData({ province: res.data.result.address_component.province });
that.setData({ city: res.data.result.address_component.city });
that.setData({ district: res.data.result.address_component.district });
that.setData({ street: res.data.result.address_component.street });
}
}
})
},