步骤
- 使用 wx.getLocation来获取当前位置:
注意;当用户取消位置获取授权之后,再次点击获取位子按钮小程序不会再提醒用户是否授权,这个时候最好自己弹出提示框让用户去设置页面开启授权设置.
wx.getLocation({
type: 'wgs84',
altitude: 'false'
success (res) {
const latitude = res.latitude
const longitude = res.longitude
const speed = res.speed
const accuracy = res.accuracy
const altitude = res.altitude
...
},
fail (err) {
},
complete(){
}
})
注意:
工具中定位模拟使用IP定位,可能会有一定误差。且工具目前仅支持 gcj02 坐标。
使用第三方服务进行逆地址解析时,请确认第三方服务默认的坐标系,正确进行坐标转换。
(后面需要使用获取到的数据打开微信内置地图,最好使用gjc02)
- 使用 wx.openLocation打开地址位置:
将经纬度传进去就可以打开地图并标记当前位置.
wx.getLocation({
type: 'gcj02',
success (res) {
const latitude = res.latitude
const longitude = res.longitude
wx.openLocation({
latitude,
longitude,
scale