uniapp中地址授权
/**判断是否授权 */
getSettingFun: function() {
let that = this;
//先通过 uni.getSetting 先查询一下用户是否授权了 "scope.userLocation" 这个 scope,也可直接调用uni.authorize请求授权;
uni.getSetting({
success: function(res) {
// res.authSetting['scope.userLocation'] == undefined 表示 初始化进入该页面
// res.authSetting['scope.userLocation'] == false 表示 非初始化进入该页面,且未授权
// res.authSetting['scope.userLocation'] == true 表示 地理位置授权
let status = res.authSetting["scope.userLocation"];
if (status != undefined && status != true) {
uni.showModal({
title: '请求授权当前位置',
content: '需要获取你的地理位置,请确认授权',
success: function(res) {
if (res.cancel) {
uni.showToast({
title: '拒绝授权',
icon: 'none',
duration: 1000
})
} else if (res.confirm) {
uni.openSetting({
success: function(res) {
console.log("[uni.openSetting]", res)
if (res.authSetting['scope.userLocation'] == true) {
uni.showToast({
title: '授权成功',
icon: 'success',
duration: 1000
})
//成功授权直接调用getlocation的API
that.getLocationFunc();
} else {
uni.showToast({
title: '授权失败',
icon: 'none',
duration: 1000
})
}
}
})
}
}
})
} else if (status == undefined) {
//首次授权(undefined)
// 发起请求用户授权
uni.authorize({
scope: 'scope.userLocation',
success: function(res) {
//成功授权直接调用getlocation的API
that.getLocationFunc();
}
})
} else {
//成功授权直接调用getlocation的API
that.getLocationFunc();
}
}
})
},
//获取位置经纬度
getLocationFunc(){
return new Promise((resolve,reject)=>{
uni.getLocation({
type:"gcj02",
success(res){
resolve(res)
}
})
})
}