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

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. 檢查當前是否已經授權
		var _this=this;
		uni.getSetting({
		    success(res) {					
				if (!res.authSetting[a]) {  //3.1 每次進入程序判斷當前是否獲得授權,如果沒有就去獲得授權,如果獲得授權,就直接獲取當前地理位置
					_this.getAuthorizeInfo()
				}else{
					_this.getLocationInfo()
				}
			}
		});
	}
}

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

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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章