电子说
进入[控制台],创建一个新应用。如果您之前已经创建过应用,可直接跳过这个步骤。
在创建的应用上点击"添加新Key"按钮,在弹出的对话框中,依次:输入应用名名称,选择绑定的服务为“HarmonyOS平台”,输入AppID,如下图所示:
注意
1个KEY只能用于一个应用(多渠道安装包属于多个应用),1个Key在多个应用上使用会出现服务调用失败。
在阅读完高德地图API服务条款后,勾选此选项,点击“提交”,完成 Key 的申请,此时您可以在所创建的应用下面看到刚申请的 Key 了。
请在当前应用的Ablitiy中使用如下代码获取
let flag = bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_SIGNATURE_INFO;
let bundleInfo = bundleManager.getBundleInfoForSelfSync(flag)
let appId = bundleInfo.signatureInfo.appId;
使用定位SDK之前,需要在 config.json 文件中进行相关权限设置,确保定位功能可以正常使用。
...
"requestPermissions": [
{
"name": "ohos.permission.APPROXIMATELY_LOCATION",
"usedScene": {
"abilities": [
"Harmony_location_demoAbility"
],
"when": "always"
}
},
{
"name": "ohos.permission.LOCATION",
"usedScene": {
"abilities": [
"Harmony_location_demoAbility"
],
"when": "always"
}
},
{
"name": "ohos.permission.LOCATION_IN_BACKGROUND",
"usedScene": {
"abilities": [
"Harmony_location_demoAbility"
],
"when": "always"
}
},
{
"name": "ohos.permission.INTERNET",
"usedScene": {
"abilities": [
"Harmony_location_demoAbility"
],
"when": "always"
}
},
{
"name": "ohos.permission.KEEP_BACKGROUND_RUNNING",
"usedScene": {
"abilities": [
"Harmony_location_demoAbility"
],
"when": "always"
}
}
]
...
从ohpm仓库获取定位包,依次添加依赖。
"dependencies": {
"@amap/amap_lbs_common": "1.0.0",
"@amap/amap_lbs_location": "1.0.0"
}
[获取Key]
AMapLocationManagerImpl.setApiKey("您的key");
AMapLocationManagerImpl.updatePrivacyShow(AMapPrivacyShowStatus.DidShow, AMapPrivacyInfoStatus.DidContain, getContext(this))
AMapLocationManagerImpl.updatePrivacyAgree(AMapPrivacyAgreeStatus.DidAgree, getContext(this))
locationManger?: AMapLocationManagerImpl;
this.locationManger = new AMapLocationManagerImpl(this.context);
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}`);
})
}
如:连续定位
let listener: IAMapLocationListener = {
onLocationChanged: (location) = > {
// todo something
}, onLocationError: (error) = > {
// todo something
}
};
let options: AMapLocationOption = {
priority: geoLocationManager.LocationRequestPriority.FIRST_FIX,
scenario: geoLocationManager.LocationRequestScenario.UNSET,
timeInterval: 2,
distanceInterval: 0,
maxAccuracy: 0,
allowsBackgroundLocationUpdates: true,
locatingWithReGeocode: false,
reGeocodeLanguage: AMapLocationReGeocodeLanguage.Chinese,
isOffset: true
}
this.locationManger?.setLocationListener(AMapLocationType.Updating,listener)
this.locationManger?.setLocationOption(AMapLocationType.Updating,options)
this.locationManger?.startUpdatingLocation()
// 停止定位
// this.locationManger?.stopUpdatingLocation()
如需在后台定位,则要在应用切入后台前调用开启后台定位任务
this.locationManger?.startContinuousTask()
回到前台可以停止后台任务
this.locationManger?.stopContinuousTask()
至此就可以看到定位相关信息。也可依据参考手册实现单次定位、最后位置等操作。
审核编辑 黄宇
全部0条评论
快来发表一下你的评论吧 !