配置工程 最后更新时间: 2024年10月18日
使用定位 SDK 之前,需要在 config.json 文件中进行相关权限设置,确保定位功能可以正常使用。
第一步,配置 module.json5
首先,声明权限
...
"requestPermissions": [
{
"name": "ohos.permission.APPROXIMATELY_LOCATION",
"reason": "$string:Harmony_location_permission_reason",
"usedScene": {
"abilities": [
"Harmony_location_demoAbility"
],
"when": "always"
}
},
{
"name": "ohos.permission.LOCATION",
"reason": "$string:Harmony_location_permission_reason",
"usedScene": {
"abilities": [
"Harmony_location_demoAbility"
],
"when": "always"
}
},
{
"name": "ohos.permission.LOCATION_IN_BACKGROUND",
"reason": "$string:Harmony_location_permission_reason",
"usedScene": {
"abilities": [
"Harmony_location_demoAbility"
],
"when": "always"
}
},
{
"name": "ohos.permission.INTERNET",
"reason": "$string:Harmony_location_permission_reason",
"usedScene": {
"abilities": [
"Harmony_location_demoAbility"
],
"when": "always"
}
},
{
"name": "ohos.permission.KEEP_BACKGROUND_RUNNING",
"reason": "$string:Harmony_location_permission_reason",
"usedScene": {
"abilities": [
"Harmony_location_demoAbility"
],
"when": "always"
}
}
]
...
第二步,在工程的oh-package.json5文件中添加定位开发包
从 ohpm 仓库获取定位包,依次添加依赖
"dependencies": {
"@amap/amap_lbs_common": ">=1.1.0",
"@amap/amap_lbs_location": ">=1.1.0"
}
第三步,初始化定位
1
导入所需模块
import { AMapLocationManagerImpl } from '@amap/amap_lbs_location';
import { AMapPrivacyAgreeStatus, AMapPrivacyInfoStatus, AMapPrivacyShowStatus } from '@amap/amap_lbs_common';
import { Permissions } from '@ohos.abilityAccessCtrl';
import common from '@ohos.app.ability.common';
import abilityAccessCtrl, { PermissionRequestResult } from '@ohos.abilityAccessCtrl';
import { BusinessError } from '@ohos.base';
2
设置 Key(获取Key),并初始化隐私政策,创建 AMapLocationManagerImpl
locationManger?: AMapLocationManagerImpl;
private context = getContext(this);
onPageShow() {
//设置Key
AMapLocationManagerImpl.setApiKey("您的key");
//初始化隐私政策
AMapLocationManagerImpl.updatePrivacyShow(AMapPrivacyShowStatus.DidShow, AMapPrivacyInfoStatus.DidContain, getContext(this))
AMapLocationManagerImpl.updatePrivacyAgree(AMapPrivacyAgreeStatus.DidAgree, getContext(this))
//创建AMapLocationManagerImpl
this.locationManger = new AMapLocationManagerImpl(this.context);
}
3
动态申请相关权限
reqPermissionsFromUser(permissions: Array<Permissions>): void {
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) {
// 用户授权,可以继续访问目标操作
} else {
// 用户拒绝授权,提示用户必须授权才能访问当前页面的功能,并引导用户到系统设置中打开相应的权限
return;
}
}
// 授权成功
}).catch((err: BusinessError) => {
console.error(`Failed to request permissions from user. Code is ${err.code}, message is ${err.message}`);
})
}
提示
在onPageShow生命周期函数中调用该方法
this.reqPermissionsFromUser([
'ohos.permission.APPROXIMATELY_LOCATION',
'ohos.permission.LOCATION',
]);
下面就可以开始请求相关定位信息,如:连续定位
4
完整代码示例
import { AMapLocationManagerImpl } from '@amap/amap_lbs_location';
import { AMapPrivacyAgreeStatus, AMapPrivacyInfoStatus, AMapPrivacyShowStatus } from '@amap/amap_lbs_common';
import { Permissions } from '@ohos.abilityAccessCtrl';
import common from '@ohos.app.ability.common';
import abilityAccessCtrl, { PermissionRequestResult } from '@ohos.abilityAccessCtrl';
import { BusinessError } from '@ohos.base';
@Entry
@Component
struct Index {
locationManger?: AMapLocationManagerImpl;
private context = getContext(this);
onPageShow() {
//设置Key
AMapLocationManagerImpl.setApiKey("您的key");
//初始化隐私政策
AMapLocationManagerImpl.updatePrivacyShow(AMapPrivacyShowStatus.DidShow, AMapPrivacyInfoStatus.DidContain, getContext(this))
AMapLocationManagerImpl.updatePrivacyAgree(AMapPrivacyAgreeStatus.DidAgree, getContext(this))
//创建AMapLocationManagerImpl
this.locationManger = new AMapLocationManagerImpl(this.context);
//获取权限
this.reqPermissionsFromUser([
'ohos.permission.APPROXIMATELY_LOCATION',
'ohos.permission.LOCATION',
]);
}
reqPermissionsFromUser(permissions: Array<Permissions>): void {
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) {
// 用户授权,可以继续访问目标操作
} else {
// 用户拒绝授权,提示用户必须授权才能访问当前页面的功能,并引导用户到系统设置中打开相应的权限
return;
}
}
// 授权成功
}).catch((err: BusinessError) => {
console.error(`Failed to request permissions from user. Code is ${err.code}, message is ${err.message}`);
})
}
build() {
}
}