uni-app uni.getLocation方法失效无报错回调函数不执行
uni-app uni.getLocation方法失效无报错回调函数不执行
示例代码:
uni.getLocation({
type: "gcj02",
success: res => {
console.log(res);
this.setLocationXy({
lat: res.latitude,
lng: res.longitude,
isIP: false
}).then(() => {
console.log("定位成功:", this.$locationXy);
if (type === 're') {
uni.showToast({
title: "位置信息已更新",
icon: 'none'
})
}
})
},
fail:err=>{
console.log('定位失败',err);
},
complete:end=>{
console.log('执行结束',end);
}
});
操作步骤:
将应用定位授权由禁止后不再询问改为每次使用时都询问后再在APP中使用uni.getLocation触发了定位授权,选择仅在应用使用时允许 然后uni.getLocation方法就会失效,无法触发success、fail、complete 回调函数。
预期结果:
可以成功调用
实际结果:
直接失效、没有任何报错也不执行任何回调函数
bug描述:
将应用定位授权由禁止后不再询问改为每次使用时都询问后再在APP中使用uni.getLocation触发了定位授权,选择仅在应用使用时允许 然后uni.getLocation方法就会失效,无法触发success、fail、complete 回调函数。
更多关于uni-app uni.getLocation方法失效无报错回调函数不执行的实战教程也可以访问 https://www.itying.com/category-93-b0.html
1 回复
更多关于uni-app uni.getLocation方法失效无报错回调函数不执行的实战教程也可以访问 https://www.itying.com/category-93-b0.html
这是一个典型的权限处理问题。在Android系统中,当用户选择“仅在应用使用时允许”定位权限时,uni.getLocation可能会因为权限状态判断异常而无法正常触发回调。
解决方案:
- 权限检查前置处理
// 先检查定位权限状态
uni.authorize({
scope: 'scope.userLocation',
success: () => {
// 有权限再执行定位
uni.getLocation({
type: "gcj02",
success: (res) => {
console.log("定位成功:", res);
},
fail: (err) => {
console.log('定位失败', err);
}
});
},
fail: () => {
// 无权限时引导用户手动开启
uni.showModal({
title: '提示',
content: '需要定位权限才能使用此功能',
success: (res) => {
if (res.confirm) {
uni.openSetting();
}
}
});
}
});
- 使用try-catch包装
try {
uni.getLocation({
type: "gcj02",
success: (res) => {
console.log("定位成功:", res);
},
fail: (err) => {
console.log('定位失败', err);
}
});
} catch (error) {
console.log('定位异常:', error);
// 异常处理逻辑
}