HarmonyOS鸿蒙Next中获取定位权限一直是失败,也没地方能打开

HarmonyOS鸿蒙Next中获取定位权限一直是失败,也没地方能打开 在获取定位权限的时候一直是失败,设置里面也没有开关,怎么打开权限呢。

let context: Context = getContext(this) as common.UIAbilityContext;
let atManager: abilityAccessCtrl.AtManager = abilityAccessCtrl.createAtManager();
// requestPermissionsFromUser会判断权限的授权状态来决定是否唤起弹窗
atManager.requestPermissionsFromUser(context, permissions).then((data: PermissionRequestResult) => {
  let grantStatus: Array<number> = data.authResults;
  let length: number = grantStatus.length;
  for (let i = 0; i < length; i++) {
    if (grantStatus[i] === 0) {
      // 用户授权,可以继续访问目标操作
      let location = geoLocationManager.getLastLocation();
    } else {
      // 用户拒绝授权,提示用户必须授权才能访问当前页面的功能,并引导用户到系统设置中打开相应的权限
      if (data.dialogShownResults != undefined && !data.dialogShownResults[0].valueOf()) {
        ToastUtil.show("请打开定位权限")
      }
      promptAction.showToast({ message: '用户拒绝定位授权' });
      return;
    }
  }
  let location = geoLocationManager.getLastLocation();
  // 授权成功
}).catch((err: BusinessError) => {
  console.error(`liwei Failed to request permissions from user. Code is ${err.code}, message is ${err.message}`);
})
let permissions: Array<Permissions> = ['ohos.permission.APPROXIMATELY_LOCATION', 'ohos.permission.LOCATION'];
try {
  this.reqPermissionsFromUser(permissions);
} catch (err) {
  this.onServiceDone(JSON.stringify({ base64: uris[0] }), complete, false)
}
{
  "name": "ohos.permission.APP_TRACKING_CONSENT", 
  "reason": "$string:oaid_reason", 
  "usedScene": {
    "abilities": [
      "EntryAbility"
    ], 
    "when": "inuse"
  }
}
{
  "name": "ohos.permission.LOCATION", 
  "reason": "$string:location", 
  "usedScene": {
    "abilities": [
      "EntryAbility"
    ], 
    "when": "inuse"
  }
}

更多关于HarmonyOS鸿蒙Next中获取定位权限一直是失败,也没地方能打开的实战教程也可以访问 https://www.itying.com/category-93-b0.html

4 回复

参考配置:

{
  "name": "ohos.permission.LOCATION",
  "reason": "$string:reason",
  "usedScene": {
    "abilities": [
      "EntryAbility"
    ],
    "when": "inuse"
  }
},
{
  "name": "ohos.permission.APPROXIMATELY_LOCATION",
  "reason": "$string:reason",
  "usedScene": {
    "abilities": [
      "EntryAbility"
    ],
    "when": "inuse"
  }
},

参考demo:

import { abilityAccessCtrl, common } from '@kit.AbilityKit'
import { BusinessError } from '@kit.BasicServicesKit'

@Entry
@Component
struct Page {

  build() {
    Column() {
      Text('获取位置权限')
        .padding('50')
        .onClick(() =>{
          // PermissionUtil.requestUserPermission(['ohos.permission.APPROXIMATELY_LOCATION','ohos.permission.LOCATION'])
          this.requestPermissions()
        })
    }
  }

  // request permissions
  requestPermissions(): void {
    let atManager: abilityAccessCtrl.AtManager = abilityAccessCtrl.createAtManager();
    atManager.requestPermissionsFromUser(getContext() as common.UIAbilityContext,
      ['ohos.permission.LOCATION', 'ohos.permission.APPROXIMATELY_LOCATION'])
      .then(() => {
        console.info("进入弹出")
        // this.mapController?.setMyLocationEnabled(true);
        // this.mapController?.setMyLocationControlsEnabled(true);
        // this.mapController?.setCompassControlsEnabled(false);
        // this.mapController?.setMyLocationStyle({ displayType: mapCommon.MyLocationDisplayType.FOLLOW });
        // geoLocationManager.getCurrentLocation().then(async (result) =>{
        // let mapPosition:
        // await map.convertCoordinate(mapCommon.CoordinateType.WGS84, mapCommon.CoordinateType.GCJ02, result);
        // AppStorage.setOrCreate('longitude', mapPosition.longitude);
        // AppStorage.setOrCreate('latitude', mapPosition.latitude);
        // let cameraPosition: mapCommon.CameraPosition ={
        // target: mapPosition,
        // zoom: 15,
        // tilt: 0,
        // bearing: 0
        // };
        // let cameraUpdate = map.newCameraPosition(cameraPosition);
        // this.mapController?.animateCamera(cameraUpdate, 1000);
        // })
      })
      .catch((err: BusinessError) =>{
        console.error(`Failed to request permissions from user. Code is ${err.code}, message is ${err.message}`);
      })
  }
}

更多关于HarmonyOS鸿蒙Next中获取定位权限一直是失败,也没地方能打开的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在系统设置里点 左上角的 编辑按钮,然后点 屏幕中间 的添加按钮,然后把 位置信息 添加到系统设置中,最后在系统设置中开启 位置信息 开关。

在HarmonyOS鸿蒙Next中,获取定位权限失败可能是由于以下原因:

  1. 权限未正确配置:确保在config.json文件中正确声明了定位权限。需要在module下的reqPermissions中添加ohos.permission.LOCATIONohos.permission.APPROXIMATELY_LOCATION权限。

  2. 设备定位服务未开启:检查设备的定位服务是否已开启。用户需要在系统设置中手动开启定位服务。

  3. 应用未请求权限:在应用中调用requestPermissionsFromUser方法请求定位权限。确保在适当的时机(如应用启动时)请求权限。

  4. 系统权限管理:检查系统权限管理中是否已授予应用定位权限。用户可能已在系统设置中禁用了应用的定位权限。

  5. API使用错误:确保使用geoLocationManager相关API时,调用了正确的接口和方法。例如,使用getCurrentLocation获取当前位置时,需确保已正确配置参数。

  6. 设备兼容性问题:某些设备可能存在兼容性问题,导致定位权限无法正常获取。检查设备是否支持鸿蒙Next的定位功能。

  7. 系统版本问题:确保设备和系统版本支持鸿蒙Next的定位功能。某些旧版本可能存在已知问题。

如需进一步排查,可参考鸿蒙开发者文档中的定位权限相关章节。

在HarmonyOS Next中,获取定位权限失败可能是由于以下原因:

  1. 应用未在config.json中正确声明定位权限;
  2. 设备的定位服务未开启;
  3. 用户未授权定位权限。

解决方法:

  1. 确保在config.json中添加ohos.permission.LOCATION权限;
  2. 在设备设置中检查并开启定位服务;
  3. 在应用首次请求定位时,确保用户已授权。

如果问题仍未解决,建议检查系统日志或联系开发支持。

回到顶部