微信小程序 引导地址授权 获取位置信息 uniapp

 概述

 获取位置信息,需要保证是否授权位置信息,有几个条件是导致无法授权的原因

(1)微信应用未授权定位设置

(2)首次进入小程序未授权位置信息

(3)小程序之前阻止过授权位置信息

(4)未打开手机定位设置

onLoad(){
    this.getAddressInfo();
},
methods:{
    //地址信息获取
    getAddressInfo(){
        let that=this;
        try{ 
            uni.getLocation({ 
                type: 'gcj02',	
                geocode:true,//设置该参数为true可直接获取经纬度 
                success: function (resF){
                    console.log(resF) 
                },
                fail: function (){ 
                    //地址获取失败提示用户执行相关操作
                    that.openSetting(); 
                }
            });
        }catch(v){
                
        }
    }
}

代码说明

uni.getLocation({

    type: 'gcj02',  

    geocode:true,//设置该参数为true可直接获取经纬度

    success: function (resF){

        console.log(resF)

    },

    fail: function (){

        //地址获取失败提示用户执行相关操作

        that.openSetting();

    }

});

uni.getLocation()会触发,小程序的位置授权

正常流程在以上四个条件都满足情况下,点击确定位置授权

就会进入success函数中,获取经纬度

否则就会进入fail函数中,进行位置授权引导

openSetting(){
    const that = this;
    uni.getSetting({
        success: (res) => {
            if (res.authSetting['scope.userLocation'] == true) {
                uni.showModal({
                    title: '提示',
                    content: '请打开定位权限',
                    success: function(res) {
                        if (res.confirm) {
                            that.getAddressInfo();
                        } else if (res.cancel) {
                            console.log('用户点击取消');
                        }
                    }
                });
            } else {
                uni.showModal({
                    title: '打开定位权限',
                    content: '是否开启授权',
                    success: res => {
                        if (res.confirm) {
                            uni.openSetting({
                                success: (res) => {
                                    let authSettings = res.authSetting
                                    if (authSettings['scope.userLocation'] == true) {
                                        that.getPageList();
                                    } else {
                                        uni.showModal({
                                            title: '提示',
                                            content: '打开定位权限,自动匹配所在城市',
                                            success: function(res) {
                                                if (res.confirm) { 
                                                    that.getAddressInfo();
                                                } else if (res.cancel) {
                                                    console.log('用户点击取消');
                                                }
                                            }
                                        });
                                    }
                                }
                            })
                        } else { 
                            uni.showToast({
                                title: '你未开通地理位置授权',
                                icon: 'none'
                            })
                        }
                    },
                })
            }
        }
    })
},

 执行以上代码,即可完成用户引导,直接使用即可

  • 12
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

路光.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值