微信小程序 引导地址授权 获取位置信息 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'
                            })
                        }
                    },
                })
            }
        }
    })
},

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

相关推荐

  1. 程序引导用户打开定位授权通用模版

    2024-01-07 16:08:02       34 阅读
  2. uniapp引入程序直播组件

    2024-01-07 16:08:02       35 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-07 16:08:02       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-07 16:08:02       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-07 16:08:02       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-07 16:08:02       18 阅读

热门阅读

  1. c++实验 引用与指针

    2024-01-07 16:08:02       41 阅读
  2. ECMAScript2015(ES6)

    2024-01-07 16:08:02       30 阅读
  3. 20240107 SQL基础50题打卡

    2024-01-07 16:08:02       44 阅读
  4. 业务数据技术中台概念与相互关系

    2024-01-07 16:08:02       37 阅读
  5. Kotlin: Jetpack — LiveData简单应用

    2024-01-07 16:08:02       52 阅读
  6. 一步一步写线程之四简单线程池

    2024-01-07 16:08:02       26 阅读
  7. 家谱管理系统的设计与实现(c语言)

    2024-01-07 16:08:02       74 阅读
  8. Socket.D 替代 http 协议像 Ajax 一样开发前端接口

    2024-01-07 16:08:02       42 阅读