uniapp 记录

158 阅读2分钟

处理IOS用户 首次拒绝地图授权后 再次授权failed

mapPlace(state) {
    // uni.authorize
    // 提前向用户发起授权请求。
    // 调用后会立刻弹窗询问用户是否同意授权小程序使用某项功能或获取用户的某些数据

    let _this = this;
    // #ifdef MP-WEIXIN

    uni.authorize({
        scope: 'scope.userLocation',
        // 成功
        success() {
            // 1 用户允许授权
            // 2 用户之前已经同意授权,则不会出现弹窗,直接返回成功
            //  以上两种情况都会进入到success回调中 
            // 获取用户当前位置 并打开地图
            uni.chooseLocation({
                    success: res => {
                    // 返回用户当前选择的位置信息;
                    // 可进行保存data 回显在表单上/提交后台
                    if (state === 'start') {
                            _this.formDate.startDestination = res.name
                    } else {
                            _this.formDate.endDestination = res.name
                    };
                    _this.addressMessage.latitude = res.latitude; 
                    _this.addressMessage.longitude = res.longitude;
                    _this.addressMessage.name = res.name;
                    _this.addressMessage.address = res.address; 
                    _this.showMap = true;
                    _this.marker[0].latitude = res.latitude; 
                    _this.marker[0].longitude = res.longitude;

                    _this.marker[1].latitude = res.latitude+0.0002;
                    _this.marker[1].longitude = res.longitude+0.0004;
                }
            });
        },
        // 失败
        fail() {
            // 1 用户拒绝授权
            // 2 用户之前拒绝了授权,此接口会直接进入失败回调
            //  以上两种情况都会进入到fail回调中
            // 一般搭配uni.getSetting和uni.openSetting使用

            // 官方文档中明确指出从2.3.0版本开始,当用户发生点击行为后才可以跳转打开设置页,不允许在用户无任何操作的情况下进行跳转
            // so报错 openSetting:fail can only be invoked by user TAP gesture.

            // 解决没办法 可通过提示框的按钮点击 触发opensetting
            //弹出框
        uni.showModal({
            title: '提示',
            content: '您拒绝了位置的授权,不可使用地图选择功能',
            showCancel: false,
            success: function(res) {
                //打开设置
                uni.openSetting({
                    success(res) {
                        let userLocation = res.authSetting['scope.userLocation'];
                        if (userLocation) {
                        // 继续进行授权成功后的操作
                        //用户开启位置权限
                        uni.chooseLocation({
                            success: res => {
                                // 返回用户当前选择的位置信息;
                                // 可进行保存data 回显在表单上/提交后台
                                if (state === 'start') {
                                    this.formDate.startDestination = res.name
                                } else {
                                    this.formDate .endDestination = res.name};
                                     console.log('位置名称:' + res.name);
                                     console.log('详细地址:' + res.address);
                                     console.log('纬度:' + res.latitude);
                                     console.log('经度:' + res.longitude);
                                }
                          });
                   } else {
                        // 用户拒接授权 给提示
                    uni.showToast({title: '您已拒绝位置授权',   duration: 2000});
                                                    }
                                            }
                                    });
                            }
                    });

            }
    })
    //#endif
},


调用百度地图页面

uni.openLocation({
        latitude: Number(this.latitude), //要去的纬度-地址
        longitude: Number(this.longitude), //要去的经度-地址
        address: this.address, //要去的具体地址
})

image.png

image.png