uniapp的微信小程序,獲取授權,獲取中文街道地理位置


w問題描述:在微信小程序模擬器上運行獲取坐標時 獲取不到信息,原因是 沒有調起默認地理位置;

解決辦法:或者在manifest.json的源碼視圖中配置:配置appid和地理位置 默認彈起獲取地理位置信息彈框;

轉載至:https://blog.csdn.net/qq_42231156/article/details/89764301

1. 詳細見代碼:在需要.vue頁面調用如下方法。

onReady(){
    this.isGetLocation();
},
methods: {
    getAuthorizeInfo(a="scope.userLocation"){  //1. uniapp彈窗彈出獲取授權(地理,個人微信信息等授權信息)彈窗
        var _this=this;
        uni.authorize({
            scope: a,
            success() { //1.1 允許授權
                _this.getLocationInfo();
            },
            fail(){    //1.2 拒絕授權
                console.log("你拒絕了授權,無法獲得周邊信息")
            }
        })
    },
    getLocationInfo(){  //2. 獲取地理位置
        var _this=this;
        uni.getLocation({
            type: 'wgs84',
            success (res) {
                console.log("你當前經緯度是:")
                console.log(res)
                let latitude,longitude;
                latitude = res.latitude.toString();
                longitude = res.longitude.toString();
                uni.request({
                    header:{
                        "Content-Type": "application/text"
                    },
                    url:'http://apis.map.qq.com/ws/geocoder/v1/?location='+latitude+','+longitude+'&key=MVGBZ-R2U3U-W5CVY-2PQID-AT4VZ-PDF35',
                    success(re) {
                        console.log("中文位置")
                        console.log(re)       
                        if(re.statusCode===200){
                            console.log("獲取中文街道地理位置成功")
                        }else{
                            console.log("獲取信息失敗,請重試!")
                        }
                     }
                });
            }
        });
    },
    isGetLocation(a="scope.userLocation"){ // 3. 檢查當前是否已經授權訪問scope屬性,參考下截圖
        var _this=this;
        uni.getSetting({
            success(res) {                    
                if (!res.authSetting[a]) {  //3.1 每次進入程序判斷當前是否獲得授權,如果沒有就去獲得授權,如果獲得授權,就直接獲取當前地理位置
                    _this.getAuthorizeInfo()
                }else{
                    _this.getLocationInfo()
                }
            }
        });
    }
}
View Code

2. 微信小程序中,目前版本無法自動直接彈窗用戶用戶信息scope.userInfo信息,需要按鈕點擊主動授權引導。

<button  open-type="getUserInfo" @getuserinfo="bindGetUserInfo" >授權登錄</button>
<!--注意,如果是小程序中則是 <button  open-type="getUserInfo" bindGetUserInfo="bindGetUserInfo" >授權登錄</button>-->
 
methods:{
     bindGetUserInfo(e) {
        if (e.detail.userInfo){
              //用戶按了允許授權按鈕
        } else {
             //用戶按了拒絕按鈕
        }
     }
}

3. uni-app配置微信小程序的appid: 開發過程中,需要在unpackage>>dist>>dev>>mp-weixin>>app.json中加入如下配置:

"permission": {
    "scope.userLocation": {
            "desc": "你的位置信息將用於小程序位置接口的效果展示"
    }
}

或者在manifest.json的源碼視圖中配置:配置appid和地理位置

"mp-weixin": { /* 小程序特有相關 */
        "appid": "", //需要配置appid
        "setting": {
            "urlCheck": false
        },
        "usingComponents": true,
        "permission": {
            "scope.userLocation": {
                "desc": "你的位置信息將用於小程序位置接口的效果展示"
            }
        }
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM