uni-app 华为手机 harmony os 4.2.0 uni.getLocation 获取到的经纬度是0
uni-app 华为手机 harmony os 4.2.0 uni.getLocation 获取到的经纬度是0
操作步骤:
- 安装app,点击左上角进入选择地址页面
预期结果:
- 预期定位uni.getLocation获取到经纬度;通过经纬度解析得到当前地理位置;显示在页面上
实际结果:
- 实际定位uni.getLocation获取的经纬度为0;导致无法通过经纬度解析地理位置
bug描述:
uniapp 华为手机 harmony os 4.2.0 uni.getLocation 获取到的经纬度是0
高德和腾讯地图都配置了;
let that = this;
uni.getLocation({
type: "gcj02",
geocode: true,
success: function(res) {
utils.showToast("定位信息"+JSON.stringify(res))
uni.request({
url: that.$http.baseUrl + that.$API.postHome_geocode(),
data: {
event: `${res.latitude},${res.longitude}`,
},
method: 'GET',
success: function(res) {
if (res.data.data.status == 1) {
var temCity = res.data.data.regeocode.addressComponent.city;
var temProvince = res.data.data.regeocode.addressComponent.province;
temCity = temCity ? (typeof temCity == 'string' ? temCity : temProvince) : "定位失败";
that.storageCity(temCity);
setTimeout(function() {
that.showAlert = false;
}, 1200);
} else {
console.error('反解析失败:', res.data);
}
},
fail: function(err) {
console.error('请求失败:', err);
}
});
},
fail: (err) => {
if (err.errMsg === "getLocation:fail:permission denied") {
utils.showToast("没有定位权限!")
console.log('没有定位权限');
} else if (err.errMsg.includes("getLocation:fail")) {
utils.showToast("定位服务可能未开启或存在其他问题!")
console.log('定位服务可能未开启或存在其他问题');
}
that.storageCity('定位失败');
}
})

更多关于uni-app 华为手机 harmony os 4.2.0 uni.getLocation 获取到的经纬度是0的实战教程也可以访问 https://www.itying.com/category-93-b0.html
1 回复
更多关于uni-app 华为手机 harmony os 4.2.0 uni.getLocation 获取到的经纬度是0的实战教程也可以访问 https://www.itying.com/category-93-b0.html
这是一个典型的HarmonyOS系统权限适配问题。在HarmonyOS 4.2.0中,即使配置了高德和腾讯地图,也需要特别注意权限获取流程。
从你的代码看,虽然处理了权限拒绝的情况,但获取到经纬度为0通常意味着定位服务已授权但实际定位失败。建议:
- 检查权限配置:确保manifest.json中已正确声明位置权限:
"permission": {
"scope.userLocation": {
"desc": "用于获取当前位置信息"
}
}
- 添加超时处理:在getLocation参数中设置timeout,避免长时间等待:
uni.getLocation({
type: "gcj02",
geocode: true,
timeout: 10000, // 10秒超时
// ...其他参数
})
- 验证系统定位服务:在调用前检查系统定位是否开启:
uni.getSystemInfo({
success: function(res) {
if (!res.locationEnabled) {
utils.showToast("请开启系统定位服务");
return;
}
// 再执行uni.getLocation
}
});

