权限说明
权限配置:在 module.json5 文件中配置 HarmonyOS NEXT 定位 SDK 所需的相关权限,确保 SDK 可以正常使用。配置如下:
"requestPermissions": [
{
"name": "ohos.permission.LOCATION",
"reason": "允许应用在前台运行时获取位置信息",
},
{
"name": "ohos.permission.LOCATION_IN_BACKGROUND",
"reason": "允许应用在后台运行时获取位置信息",
},
{
"name": "ohos.permission.APPROXIMATELY_LOCATION",
"reason": "允许应用获取设备模糊位置信息",
},
{
"name": "ohos.permission.APP_TRACKING_CONSENT",
"reason": "允许应用获取设备唯一标识符",
},
{
"name": "ohos.permission.GET_WIFI_INFO",
"reason": "允许应用获取连接wifi信息",
},
{
"name": "ohos.permission.GET_NETWORK_INFO",
"reason": "允许应用获取网络信息",
},
{
"name": "ohos.permission.INTERNET",
"reason": "允许应用访问网络",
},
{
"name": "ohos.permission.KEEP_BACKGROUND_RUNNING",
"reason": "允许应用进行长时任务",
}
]
权限申请:在应用入口开始主动申请定位权限
let atManager = abilityAccessCtrl.createAtManager();
try {
atManager.requestPermissionsFromUser(this.context,
['ohos.permission.LOCATION', 'ohos.permission.APPROXIMATELY_LOCATION'])
.then((data) => {
hilog.info(0x0000, TAG, `data: ${JSON.stringify(data)}`);
})
.catch((err: BusinessError) => {
hilog.error(0x0000, TAG, `err: ${JSON.stringify(err)}`);
})
} catch (err) {
hilog.error(0x0000, TAG, `catch err->${JSON.stringify(err)}`);
}