小程序检测获取并引导授权位置

author () {
           const that =this
           uni.authorize({
                   scope: 'scope.userLocation',
                   success(){ //1.1 允许授权
                      uni.getLocation({
                         type: 'gcj02', //返回可以用于uni.openLocation的经纬度
                         success: function (res) {
                       that.latitude = res.latitude;
                       that.longitude = res.longitude;
                       that.sub()
                         }
                       })
                        },
                       fail(){    //1.2 拒绝授权
                           uni.showModal({
                               content:'检测到您没打开获取信息功能权限,是否去设置打开?',
                                   confirmText: "确认",
                                   cancelText:'取消',
                                   success: (res) => {
                                       if(res.confirm){
                                           uni.openSetting({
                                               success: (res) => {
                                                   console.log(res);
                                                   that.getLocation();
                                               }
                                           })
                                       }else{
                                           console.log('取消');
                                           return false;
                                       }
                                   }
                           })                                  
                           return false;
                       }
                   })
       },
getLocation(){
            uni.getLocation({
                    type: 'gcj02', //返回可以用于uni.openLocation的经纬度
                    success: function (res) {
                    console.log('当前位置的经度:' + res.longitude);
                    console.log('当前位置的纬度:' + res.latitude);
                            this.latitude = res.latitude;
                            this.longitude = res.longitude;     
                    }
            });
        },
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容