1、关于获取用户授权得到地理位置的代码:
<view bindtap="scanCode">
点击按钮
</view>
var QQMapWX = require('../../utils/qqmap-wx-jssdk.js'); //1、必须引入
var qqmapsdk;
Page({
/**
* 页面的初始数据
*/
data: {
scanCodeMsg: "",
address:'',
latitude:'',
longitude:'',
},
/**
* 按钮绑定的方法
*/
scanCode(){
let _this = this;
_this.getUserLocation();
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
// 实例化API核心类
qqmapsdk = new QQMapWX({ //2、你申请的密钥
key: '你申请的密钥'
});
},
getUserLocation: function() {
let vm = this;
wx.getSetting({
success: (res) => {
if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {
wx.showModal({
title: '请求授权当前位置',
content: '需要获取您的地理位置,请确认授权',
success: function(res) {
if (res.cancel) {
wx.showToast({
title: '拒绝授权',
icon: 'none',
duration: 1000
})
} else if (res.confirm) {
wx.openSetting({
success: function(dataAu) {
if (dataAu.authSetting["scope.userLocation"] == true) {
wx.showToast({
title: '授权成功',
icon: 'success',
duration: 1000
})
//再次授权,调用wx.getLocation的API
vm.getLocation();
} else {
wx.showToast({
title: '授权失败',
icon: 'none',
duration: 1000
})
}
}
})
}
}
})
} else if (res.authSetting['scope.userLocation'] == undefined) {
//调用wx.getLocation的API
vm.getLocation();
} else {
//调用wx.getLocation的API
vm.getLocation();
}
}
})
},
// 微信获得经纬度
getLocation: function() {
let vm = this;
wx.getLocation({ //不要冤枉他,他定位的其实还蛮准的
type: 'wgs84',
isHighAccuracy:true,
success: function(res) {
var latitude = res.latitude
var longitude = res.longitude
var speed = res.speed // 速度,以米/每秒计
var accuracy = res.accuracy; // 位置精度
vm.getLocal(latitude, longitude)
},
fail: function(res) {
console.log('fail' + JSON.stringify(res))
}
})
},
// 获取当前地理位置
getLocal: function(latitude, longitude) {
let vm = this;
qqmapsdk.reverseGeocoder({
location: {
latitude: latitude,
longitude: longitude,
},
coord_type:1,
get_poi:1,
poi_options: 'policy=2;radius=600;page_size=20;page_index=1', //3、这是重点!!!!
success: function(res) {
// let address = res.result.address
let province = res.result.ad_info.province
let city = res.result.ad_info.city
let formatted_addresses = res.result.formatted_addresses.recommend
let address = province + city + formatted_addresses
vm.setData({
address:address,
// province: province,
// city: city,
latitude: latitude,
longitude: longitude
})
},
fail:function(data){
console.log(data);
}
});
},
})
2、重点就在于这一段代码:
coord_type:1,
get_poi:1,
poi_options: 'policy=2;radius=600;page_size=20;page_index=1', //半径,取值范围 1-5000(米)policy=2 到家场景:筛选合适收货的poi,并会细化收货地址,精确到楼栋;
3、文档解析:微信小程序JavaScript SDK | 腾讯位置服务