uni-app小程序,没有地址权限需要重新获取权限

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);
  })
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值