小程序引导用户授权位置信息 获取用户定位提高位置获取友好用户体验(uniapp)

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

你可能感兴趣的:(javascript,前端,vue.js)