Commit 7be6dab7 authored by 肖健's avatar 肖健

添加微信获取地理位置权限

parent b652d9ed
...@@ -63,7 +63,12 @@ ...@@ -63,7 +63,12 @@
"setting" : { "setting" : {
"urlCheck" : false "urlCheck" : false
}, },
"usingComponents" : true "usingComponents" : true,
"permission" : {
"scope.userLocation" : {
"desc" : "你的位置信息将用于定位"
}
}
}, },
"mp-alipay" : { "mp-alipay" : {
"usingComponents" : true "usingComponents" : true
......
...@@ -163,8 +163,100 @@ export default { ...@@ -163,8 +163,100 @@ export default {
} }
}) })
}, },
// 用户授权
getAuthorize() {
const _this = this
uni.authorize({
scope: 'scope.userLocation', // 获取地理信息必填的参数,其它参数见文档
success(res) {
_this.getLocation()
},
// 授权失败
fail(err) {
uni.showModal({
title: '温馨提示',
content: '无法获取当前位置,请手动开启授权',
success: function (res) {
if (res.confirm) {
console.log('用户点击确定')
uni.openSetting({
success(res) {
if (res.authSetting['scope.userLocation']) {
console.log('打开授权设置定位')
_this.getLocation()
}
},
})
} else if (res.cancel) {
console.log('用户点击取消')
}
},
})
},
})
},
// 获取用户的地理位置,
getLocation() {
const _this = this
uni.getLocation({
type: 'wgs84',
altitude: true,
success(res) {
//小程序只能获取到位置经纬度,需要借助高德地图的逆解析地址,显示出中文具体城市名字
_this.loadCity(res.longitude, res.latitude)
},
fail() {
console.log('拒绝获取地理位置')
}
})
},
// 经纬度转具体城市
// 1)需要用到高德地图的逆地理编码
// 2)需要当前地理位置的经纬度
// 3)小程序的key值
loadCity(longitude, latitude) {
const _this = this
uni.request({
header: {
'Content-Type': 'application/text',
},
//注意:这里的key值需要高德地图的 web服务生成的key 只有web服务才有逆地理编码
url: 'https://restapi.amap.com/v3/geocode/regeo?output=JSON&location=' +
longitude +
',' +
latitude +
'&key=1bba2ab2de600f8b5fafe167e09cd2af&radius=1000&extensions=all',
success(res) {
console.log(res);
if (res.statusCode === 200) {
if(res.data.status === '1') {
let addressInfo = res.data
//详细地址
_this.detailInfo = addressInfo.regeocode.formatted_address
//省
_this.provinceName = addressInfo.regeocode.addressComponent.province
//市,直辖市有可能为空数组
_this.cityName = addressInfo.regeocode.addressComponent.city
//为数组的时候设置为空
if(Array.isArray(cityName)) {
_this.cityName = ''
}
//区
_this.countyName = addressInfo.regeocode.addressComponent.district
console.log("当前位置: " + _this.detailInfo + " " + _this.provinceName + " " + " " + _this.cityName + " " + _this.countyName)
}
} else {
console.log('获取信息失败,请重试!')
}
},
})
}
}, },
onLoad() { onLoad() {
// 在页面加载的时候去判断用户有没有授权地理位置,没授权的话引导用户去设置页手动授权
this.getAuthorize()
if(e.query) { if(e.query) {
//获取二维码携带的参数 //获取二维码携带的参数
//扫码后得到的参数 //扫码后得到的参数
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment