uni-app 应用页面手动打开定位授权给与权限但是 uni.getLocation 报错
uni-app 应用页面手动打开定位授权给与权限但是 uni.getLocation 报错
示例代码:
import modal from "./modal.js"
export default{
// 获取地理位置
location:(() =>{
return new Promise(resolve => {
uni.getLocation({
type: 'wgs84',
geocode:true,
success: function (res) {
console.log(res)
resolve(res)
},
fail: function(err) {
// console.log(err); // 用户未开启GPS,可能获取不到
resolve("获取定位失败,是否授权打开定位")
modal.showModal({content: `获取定位失败,是否授权打开定位`}).then((data) => {
uni.getSystemInfo({
success: (sys) =>{
if(sys.platform=='ios'){
plus.runtime.openURL("app-settings://");
}else{
var main = plus.android.runtimeMainActivity();
var Intent = plus.android.importClass("android.content.Intent");
//可能应该直接进入应用列表的权限设置?=> android.settings.APPLICATION_SETTINGS
var mIntent = new Intent('android.settings.LOCATION_SOURCE_SETTINGS');
main.startActivity(mIntent);
}
}
})
}).catch((reject) => {
});
}
});
});
});
操作步骤:
// 定位-获取详情位置
getLocation() {
let that = this
this.$getLocation().then((res)=>{
console.log(res)
that.longitude = res.longitude
that.latitude = res.latitude
that.covers[0].latitude = res.latitude
that.covers[0].longitude = res.longitude
var point = new plus.maps.Point(res.longitude, res.latitude);
plus.maps.Map.reverseGeocode(
point, {},
function(event) {
var address = event.address; // 转换后的地理位置
var point = event.coord; // 转换后的坐标信息
var coordType = event.coordType; // 转换后的坐标系类型
that.address = address
},
function(e) {}
);
})
预期结果:
手动打开授权页面进行授权 返回后uni.getLocation 反正成功的值
实际结果:
uni.getLocation 报错
bug描述:
App端 定位授 权问题 uni.getLocation在 app.vue 页面内拒绝定位权限,手动再次打开定位权限页面进行授权 uni.getLocation可以正常获取定位数据 。 拒绝授权的前提下在子页面进行手动打开定位权限页面进行授权 在返回子页面 uni.getLocation 报错
| 信息类别 | 详细信息 |
|---|---|
| 产品分类 | uniapp/App |
| PC开发环境操作系统 | Windows |
| PC开发环境操作系统版本号 | 64位 |
| HBuilderX类型 | 正式 |
| HBuilderX版本号 | 3.1.22 |
| 手机系统 | Android |
| 手机系统版本号 | Android 11 |
| 手机厂商 | vivo |
| 手机机型 | iqooneo855 |
| 页面类型 | vue |
| 打包方式 | 云端 |
| 项目创建方式 | HBuilderX |
更多关于uni-app 应用页面手动打开定位授权给与权限但是 uni.getLocation 报错的实战教程也可以访问 https://www.itying.com/category-93-b0.html
7 回复
顶顶顶顶顶顶顶顶
更多关于uni-app 应用页面手动打开定位授权给与权限但是 uni.getLocation 报错的实战教程也可以访问 https://www.itying.com/category-93-b0.html
我也遇到这个问题了,楼主解决了吗
没 等待官方解答中
具体内容请看视频
ios 目前没有问题 安卓出现该问题
这个问题是由于 Android 权限管理机制导致的。在 App 端,当用户在子页面手动开启定位权限后返回时,应用需要重新获取权限状态并初始化定位服务。
解决方案:
- 添加权限检查逻辑:在调用
uni.getLocation前,先检查定位权限状态。
// 检查定位权限
checkLocationPermission() {
return new Promise((resolve) => {
uni.authorize({
scope: 'scope.userLocation',
success: () => resolve(true),
fail: () => resolve(false)
});
});
}
- 修改定位获取逻辑:在获取定位前先检查权限,如果无权限则引导用户开启。
location: (() => {
return new Promise(async (resolve) => {
// 先检查权限
const hasPermission = await this.checkLocationPermission();
if (!hasPermission) {
// 请求权限
uni.showModal({
title: '提示',
content: '需要定位权限才能使用此功能',
success: (res) => {
if (res.confirm) {
uni.openSetting({
success: (settingRes) => {
if (settingRes.authSetting['scope.userLocation']) {
// 权限已开启,重新获取定位
this.getLocationDirectly(resolve);
}
}
});
}
}
});
return;
}
// 已有权限,直接获取定位
this.getLocationDirectly(resolve);
});
}),
// 直接获取定位的方法
getLocationDirectly(resolve) {
uni.getLocation({
type: 'wgs84',
geocode: true,
success: (res) => {
console.log(res);
resolve(res);
},
fail: (err) => {
console.error('获取定位失败:', err);
resolve(null);
}
});
}
- 添加页面生命周期处理:在子页面的
onShow生命周期中重新检查权限状态。
onShow() {
// 页面显示时检查定位权限状态
this.checkAndRequestLocation();
}
methods: {
async checkAndRequestLocation() {
const hasPermission = await this.checkLocationPermission();
if (hasPermission) {
// 重新获取定位
this.getLocation();
}
}
}

