在app.json中添加:
"permission": {
"scope.userLocation": {
"desc": "请求获取位置信息"
}
}
加了个提示框,用户点击确定时获取地理位置:
wx.showModal({
title: '提示',
content: '请求获取位置信息',
success(res) {
if (res.confirm) {
wx.getLocation({
type: 'wgs84',
success(res) {
const latitude = res.latitude
const longitude = res.longitude
const speed = res.speed
const accuracy = res.accuracy
console.log("纬度" + latitude + "\n经度" + longitude + "\n速度" + speed + "\n位置精确度" + accuracy);
},
fail(res){
wx.showToast({
title: '请打开位置信息',
})
}
})
} else if (res.cancel) {
console.log('用户点击取消')
}
}
})