许多开发者在使用微信小程序时,可能会遇到定位功能在华为鸿蒙4.0系统上失效的问题。本文将分析并解决一个在华为鸿蒙4.0系统上出现的微信小程序定位故障,该问题表现为:小程序定位功能在华为鸿蒙4.0系统上间歇性失效,iOS系统则运行正常;失败时,控制台有时仅打印“start”,有时无任何打印信息。

问题代码片段使用了uni.getlocation()方法:
onShow(){
this.glApp.doLocation((res) => {
this.getList()
})
}
export const doLocation = (completed) => {
console.log("start")
uni.getLocation({
isHighAccuracy: true,
type: 'gcj02',
success(res) {
console.log("succcc")
uni.setStorageSync('locMess', res)
completed(res)
},
fail(res) {
console.log("errrr")
uni.removeStorageSync('locMess')
uni.hideLoading()
uni.getSetting({
success(res) {
console.log(res.authSetting)
if (res.authSetting['scope.userLocation'] == false) {
uni.showModal({
// ... (弹窗代码) ...
});
} else {
uni.showToast({
// ... (提示代码) ...
})
}
}
})
}
})
}代码中,doLocation 函数使用 uni.getLocation() 获取位置信息。失败时,会检查用户定位权限,并引导用户授权。但该代码在华为鸿蒙4.0系统上表现不稳定。
建议尝试使用微信小程序原生 API wx.getLocation() 替代 uni.getLocation(),以排除问题是否源于 uni-app 框架或鸿蒙系统兼容性问题。 切换到原生 API 能更有效地定位问题根源,从而找到更精确的解决方案。 这有助于确定问题是uni-app框架的兼容性问题,还是与鸿蒙系统本身的交互问题。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号