**
获取当前位置附近的小吃店功能:
**
源码:https://blog.csdn.net/BOY017/article/details/105891141
1、注册腾讯地图服务:申请秘钥
https://lbs.qq.com/dev/console/key/manage
2、下载qqmap-wx-jssdk文件并引入
var QQMapWX = require('../../utils/qqmap-wx-jssdk.js');
var qqmapsdk;
3、添加合法域名:
“ https://apis.map.qq.com ”
微信服务平台设置:添加到request域名下
注意:手机真机测试时,报错:request:fail url not in domain list ( 请求:失败url不在域列表中).需要多刷新几遍项目配置的域名信息
4、实例:
data: {statusBarHeight: app.globalData.statusBarHeight,page: 1,pois: [],result: [],latitude: '',longitude: '' }, onLoad: function () {qqmapsdk = new QQMapWX({key: '秘钥'}) }, onShow: function () {let vm = this;vm.getUserLocation(); }, //授权位置信息 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})// vm.BackTap2()} else if (res.confirm) {wx.openSetting({success: function (dataAu) {if (dataAu.authSetting["scope.userLocation"] == true) {wx.showToast({title: '授权成功',icon: 'success',duration: 1000})//再次授权,调用wx.getLocation的APIvm.getLocation();} else {wx.showToast({title: '授权失败',icon: 'none',duration: 1000})// vm.BackTap2()}}})}}})} else if (res.authSetting['scope.userLocation'] == undefined) {//调用wx.getLocation的APIvm.getLocation();}else {//调用wx.getLocation的APIvm.getLocation();}}}) },//微信获得经纬度
getLocation: function () {let vm = this;wx.getLocation({type: 'wgs84',success: function (res) {console.log('获得经纬度:',JSON.stringify(res))var latitude = res.latitudevar longitude = res.longitudevm.setData({latitude: latitude,longitude: longitude})vm.getLocal(latitude, longitude)},fail: function (res) {// vm.BackTap2()}}) },//获取当前地理位置 getLocal: function (latitude, longitude) {let that = this;wx.showLoading({title: '加载中',duration: 3000})//获取当前位置信息qqmapsdk.reverseGeocoder({location: {latitude: latitude,longitude: longitude,},success:function (params) {console.log('params',params)that.setData({result:params.result})}}),//获取附近商家列表qqmapsdk.search({location: {latitude: latitude,longitude: longitude,},keyword:'小吃',page_size:10,page_index:that.data.page,header: {'content-type': 'application/json' // 默认值},success: function (res) {console.log('地理位置:',res);wx.hideLoading()let pois = res.datathat.setData({pois: that.data.pois.concat(pois),})},fail: function (res) {console.log(res);},complete: function (res) {// console.log(res);}}); },//点击进入地图 turn_map:function (e) {console.log('e',e)wx.openLocation({latitude: e.currentTarget.dataset.index.lat,longitude: e.currentTarget.dataset.index.lng,scale: 10, //缩放比例,范围5~18name:e.currentTarget.dataset.name,address:e.currentTarget.dataset.address,})console.log('e',e) },//下拉获取数据页数 onReachBottom:function(){let vm = this;vm.setData({page:vm.data.page+1})vm.getLocal(vm.data.latitude, vm.data.longitude) }