首先呢我们要实现的功能是小程序调用 wx.getLocation api去拿用户的经纬度,拿到经纬度在调用百度接口解析地址
现在给大家演示:
bmap-wx.min.js 去官方文档下载即可 http://lbsyun.baidu.com/index.php?title=wxjsapi
//index.js
//获取应用实例
const app = getApp()
const bmap = require('../../../utils/bmap-wx.min.js');
Page({
data: {
wxMarkerData:[]
longitude: '', //经度
latitude: '', //纬度
},
onLoad(){
var that = this;
var BMap = new bmap.BMapWX({
ak: '百度地图申请的ak'
});
wx.getLocation({
type: 'wgs84',
success: function (res) {
that.setData({
latitude: res.latitude,//经度
longitude: res.longitude//纬度
})
console.log(res,'经纬度')
BMap.regeocoding({
location: that.data.latitude + ',' + that.data.longitude,
success: function (res) {
console.log(res)
that.setData({
wxMarkerData : res.wxMarkerData //地址信息
})
},
fail: function () {
wx.showToast({
title: '请检查位置服务是否开启',
})
},
});
},
fail: function () {
console.log('小程序得到坐标失败')
}
})
}
})
喜欢上方小程序,需要源码的,私信小编留下邮箱。