onShow(e) {
this.isGetLocation();
},
// 是否拥有权限
isGetLocation(a="scope.userLocation"){ // 3. 检查当前是否已经授权访问scope属性,参考下截图
var _this = this;
uni.getSetting({
success(res) {
if (!res.authSetting[a]) { //3.1 每次进入程序判断当前是否获得授权,如果没有就去获得授权,如果获得授权,就直接获取当前地理位置
_this.getAuthorizeInfo()
}else{
_this.getLocation1()
}
}
});
},
// 让用户去开启权限
getAuthorizeInfo(a="scope.userLocation"){ //1. uniapp弹窗弹出获取授权(地理,个人微信信息等授权信息)弹窗
var _this = this;
uni.authorize({
scope: a,
success() { //1.1 允许授权
_this.getLocation();
},
fail(){ //1.2 拒绝授权
showModalWithOptional("微信小程序需要您的位置信息,请前往开启", false, (res) => {
if(res.confirm) {
uni.openSetting({});
}
})
}
})
},
// 询问
showModalWithOptional(text,isCancel,successFunc){
uni.showModal({
showCancel: isCancel,
title: "温馨提示",
content:text,
confirmColor: "#E02020",
success: (res) => {
successFunc(res)
}
});
},
// 获取地址
async getLocation1() {
// 获取用户当前定位的经纬度
const { latitude,longitude } = await getLocation({ type: 'GCJ-02'})
},
// 获取当前用户定位
getLocation(object = {}) {
return new Promise((resolve, reject) => {
object.success = (...args) => {
resolve(...args)
};
object.fail = (error) => {
reject({message: '定位失败,请确定手机已开启定位功能'})
};
object.complete = () => {
}
uni.getLocation(object);
})
}
uni-app小程序,没有地址权限需要重新获取权限
最新推荐文章于 2025-03-28 16:11:16 发布