首先在app.json声引入
"permission": {
"scope.userLocation": {
"desc": "确认自己的骑行位置"
}
},
"requiredPrivateInfos": ["getLocation", "chooseLocation"],
"sitemapLocation": "sitemap.json"
获取当前地理位置 wx.getLocation
//获取当前地理位置
wx.getLocation({
type: 'gcj02', //默认为 wgs84 返回 gps 坐标,gcj02 返回可用于wx.openLocation的坐标
success: (res) => {
console.log(res)
that.setData({
latitude:res.latitude, //纬度
longitude:res.longitude, //经度
})
},
fail: (err) => {
console.log(err)
}
})
选择地理位置获取坐标 wx.chooseLocation
wxml
<button class="btn" type="default" bindtap="handleChooseLocation">
搜索选中地址
</button>
js
// 选择地理位置
handleChooseLocation: function () {
wx.chooseLocation({
success: (res) => {
console.log(res)
this.setData({
latitude:res.latitude, //纬度
longitude:res.longitude, //经度
name:res.name,
address:res.address
})
},
fail: (err) => {
console.log(err)
}
});
},
去导航 wx.getLocation
wxml
<!-- 导航门店 -->
<!-- <button class="btn" type="default" bindtap="handleOpenLocation">
去导航
</button> -->
js
// 导航到目的地
// handleOpenLocation: function () {
// wx.getLocation({
// type: 'gcj02', //返回可以用于 wx.openLocation 的经纬度
// success (res) {
// const latitude = 23.099994 //需要导航到的地址纬度
// const longitude = 113.324520 //需要导航到的地址经度
// wx.openLocation({
// latitude,
// longitude,
// name:'',
// scale: 18
// })
// }
// })
// },