小程序获取经纬度为00
- 当前 Bug 的表现(可附上截图)
获取当前位置的经纬度,经纬度的值为00
- 预期表现
经纬度的值不能为空,业务需求是必要条件
- 复现路径
代码片段如下:
//获取地理位置信息
getlocations(){
let that = this;
wx.getSetting({
success(res) {
console.log('用户是否授权地理位置信息==='+res.authSetting['scope.userLocation']);
if (!res.authSetting['scope.userLocation']) {
wx.authorize({
scope: 'scope.userLocation',
success() {
wx.getLocation({
type: 'gcj02',
success: function (res) {
console.log(res);
that.latitude = res.latitude;
that.longitude = res.longitude;
console.log('当前经度',that.latitude);
console.log('当前纬度',that.longitude);
let speed = res.speed;
let accuracy = res.accuracy;
that.gaodeMap();
},
})
},
fail(){
wx.showModal({
title: '请授权当前位置',
content: '需要获取您的地理位置,请确认授权',
success: function(res) {
if (res.confirm) {
wx.openSetting({
success: function (data) {
if (data.authSetting["scope.userLocation"] === true) {
wx.showToast({
title: '授权成功',
icon: 'success',
duration: 1000
})
that.getlocations();
//授权成功之后,再调用chooseLocation选择地方
} else {
wx.showToast({
title: '授权失败',
icon: 'success',
duration: 2000
})
that.getlocations();
}
}
})
console.log('用户点击确定')
} else if (res.cancel) {
that.getlocations();
console.log('用户点击取消')
}
}
})
// that.showModel();
}
})
}
else{
wx.getLocation({
type: 'gcj02',
success: function (res) {
console.log(res);
that.latitude = res.latitude;
that.longitude = res.longitude;
console.log('当前经度',that.latitude);
console.log('当前纬度',that.longitude);
let speed = res.speed;
let accuracy = res.accuracy;
that.gaodeMap();
},
})
}
}
})
},
- 提供一个最简复现 Demo