methods
getLocations(){
let _this = this
wx.getSetting({
success(res) {
debugger
// 判断用户是否授权过,
// 未授权过、
if (!res.authSetting['scope.userLocation']) {
wx.authorize({
scope: 'scope.userLocation',
success(res) {
// 用户允许 查看自己的位置
console.log(res);
_this.wxGetLocation()
},
fail(err) {
console.log(err);
// 当用户拒绝 查看自己的位置的时候 统计数据
// 统计数据 地理位置为空
_this.count();
}
})
} else {
// 授权过
_this.wxGetLocation();
}
},
fail(err) {
console.log(err)
}
});
},
wxGetLocation(){
let _this = this;
wx.getLocation({
// type: 'wgs84',
success: function (res) {
let latitude,longitude;
latitude = res.latitude.toString();
longitude = res.longitude.toString();
wx.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: function(res) {
_this.country = res.data.result.address_component.nation
_this.province =res.data.result.address_component.province
_this.city = res.data.result.address_component.city
_this.district = res.data.result.address_component.district
//_this.count();
// console.log(res.data.result.address_component.nation,res.data.result.address_component.province,res.data.result.address_component.city,res.data.result.address_component.district)
}
});
},
fail: function(err) {
console.log(err);
console.log(112)
}
});
},
上面方法的弊端就是,如果用户拒绝后,一段时间内无法继续让用户授权,如果想继续让用户授权,可以用open-type="openSetting"
方法,直接调用openSetting
这个方法不推荐用了,因为微信马上就把它废了,我也就不写了。
因为我的项目,获取地理位置不是太重要而且页面上没有地方放button按钮,所以就没做这个功能,在这里简单解释一下。
<button open-type="openSetting" @opensetting="handler">打开授权</button>
handler(e){
if (e.detail.authSetting["scope.userLocation"]){
//e.detail.authSetting 这个是小程序的,mpvue自己打断点找一下吧,原谅我有点懒
//if (!res.authSetting["scope.userInfo"] || !res.authSetting["scope.userLocation"])
}
}