问题描述:在微信开发者工具和真机调试时,uni.getLocation()获取定位失败,一直fail()。
问题原因:
微信官方文档,从基础库 2.117.0版本起,将对getLocation()接口增加频率限制。改为使用onLocationChange()。
代码:
getDingwei(){//获取当前设备定位
let that = this;
uni.getSetting({
success(res) {
//地理位置
//#ifdef MP-WEIXIN
if (res.authSetting['scope.userLocation']) {
uni.authorize({
scope: 'scope.userLocation',
success(res) {
uni.showLoading({
title: '正在加载中...',
mask: true
});
// 从基础库 2.117.0版本起,将对getLocation()接口增加频率限制。故改为onLocationChange()。
wx.startLocationUpdate({
success: res => {
console.log('开启小程序接收位置消息成功')
wx.onLocationChange(function (res){
// console.log(res.latitude,res.longitude)
// 3、经纬度转换成详细地址
that.confirmClockloadCity(res.longitude,res.latitude)
})
},
fail: err => console.error('开启小程序接收位置消息失败:', err),
complete: msg => console.log('调用开启小程序接收位置消息API完成')
})
// 以前的方法
// uni.getLocation(function (res) {
// type: 'gcj02',
// success:function (res) {
// // 3、经纬度转换成详细地址
// console.log("----66----");
// that.confirmClockloadCity(res.longitude,res.latitude)
// },
// fail(err) {
// console.log("----fail777----",err);
// uni.showModal({
// title: '提示',
// content: '定位失败,你未开启定位权限,点击开启定位权限',
// success: function (res) {
// if (res.confirm) {
// uni.openSetting({
// success: function (res) {
// if (res.authSetting['scope.userLocation']) {
// uni.showLoading({
// title: '正在加载中...',
// mask: true
// });
// uni.getLocation({
// type: 'gcj02',
// success:function (res) {
// that.confirmClockloadCity(res.longitude,res.latitude)
// }
// })
// } else {
// console.log('用户未同意地理位置权限')
// }
// }
// })
// }
// }
// })
// },
// complete() {
// console.log("----complete888----");
// }
// })
},
fail() {
uni.showModal({
title: '提示',
content: '定位失败,你未开启定位权限,点击开启定位权限',
success: function (res) {
if (res.confirm) {
uni.openSetting({
success: function (res) {
if (res.authSetting['scope.userLocation']) {
console.log('5555*')
uni.showLoading({
title: '正在加载中...',
mask: true
});
uni.getLocation({
type: 'gcj02',
success:function (res) {
that.confirmClockloadCity(res.longitude,res.latitude)
}
})
} else {
console.log('用户未同意地理位置权限')
}
}
})
}
}
})
}
})
} else {
console.log('用户未同意地理位置权限')
uni.showModal({
title: '提示',
content: '定位失败,你未开启定位权限,点击开启定位权限',
success: function (res) {
if (res.confirm) {
uni.openSetting({
success: function (res) {
if (res.authSetting['scope.userLocation']) {
// console.log('5555*')
uni.showLoading({
title: '正在加载中...',
mask: true
});
uni.getLocation({
type: 'gcj02',
success:function (res) {
that.confirmClockloadCity(res.longitude,res.latitude)
}
})
} else {
// console.log('00000*')
console.log('用户未同意地理位置权限')
}
}
})
}
}
})
}
//#endif
}
})
},
confirmClockloadCity: function (longitude, latitude) {//调高德地图API,用经纬度反推详细的街道地址
let that = this
uni.request({
url: 'https://restapi.amap.com/v3/geocode/regeo',
data: {
key: 'c8d499635271ab4f9d449d35911e2cf1',
location: longitude + "," + latitude,
extensions: "all",
},
success: function (res) {
var adressStr = res.data.regeocode.formatted_address
that.endServiceParamObj.adress = adressStr;
that.endServicePost();// 调结束服务的接口
},
fail: function (res) {
console.log('获取地理位置失败')
}
})
},
参考的其他答案:
https://ask.dcloud.net.cn/question/90273