1.在app.json中配置
"permission":{
"scope.userLocation":{
"desc":"你的位置信息授权"
}
}
2.在.wxml中
<map latitude="{{lat}}" longitude="{{lon}}"></map>
<button type="primary" bindtap="getLocation">获取位置</button>
<view>纬度:{{lat}}</view>
<view>经度:{{lon}}</view>
<view>精确度:{{accuracy}}</view>
3.在.js文件中
getLocation(){
let that = this
wx.getLocation({
altitude: 'true',
highAccuracyExpireTime: 5000,
isHighAccuracy: true,
type: 'gcj02',
success: (result) => {
that.setData({
lat:result.latitude,
lon:result.longitude,
accuracy:result.accuracy
})
},
fail: (res) => {},
complete: (res) => {},
})
},
参数说明:
4.显示
注意,这里需要真机调试,在微信开发者工具中由于用的是浏览器IP定位,只能定位到市级
到这里基本完成!!!