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

概述

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

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

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

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

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

javascript 复制代码
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函数中,进行位置授权引导

javascript 复制代码
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'
                            })
                        }
                    },
                })
            }
        }
    })
},

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

相关推荐
lyj16899720 分钟前
vue-i18n+vscode+vue 多语言使用
前端·vue.js·vscode
racerun1 小时前
小程序导航设置更多内容的实现方法
小程序
说私域1 小时前
基于开源AI智能名片链动2+1模式S2B2C商城小程序的超级文化符号构建路径研究
人工智能·小程序·开源
mg6681 小时前
微信小程序入门实例_____快速搭建一个快递查询小程序
微信小程序·小程序
小白变怪兽2 小时前
一、react18+项目初始化(vite)
前端·react.js
ai小鬼头2 小时前
AIStarter如何快速部署Stable Diffusion?**新手也能轻松上手的AI绘图
前端·后端·github
程序员柳2 小时前
基于微信小程序的校园二手交易平台、微信小程序校园二手商城源代码+数据库+使用说明,layui+微信小程序+Spring Boot
数据库·微信小程序·layui
墨菲安全3 小时前
NPM组件 betsson 等窃取主机敏感信息
前端·npm·node.js·软件供应链安全·主机信息窃取·npm组件投毒
GISer_Jing3 小时前
Monorepo+Pnpm+Turborepo
前端·javascript·ecmascript