如何申請key
1、創(chuàng)建新應(yīng)用
進入[控制臺],創(chuàng)建一個新應(yīng)用。如果您之前已經(jīng)創(chuàng)建過應(yīng)用,可直接跳過這個步驟。


2、添加新Key
在創(chuàng)建的應(yīng)用上點擊"添加新Key"按鈕,在彈出的對話框中,依次:輸入應(yīng)用名名稱,選擇綁定的服務(wù)為“HarmonyOS平臺”,輸入AppID,如下圖所示:
注意
1個KEY只能用于一個應(yīng)用(多渠道安裝包屬于多個應(yīng)用),1個Key在多個應(yīng)用上使用會出現(xiàn)服務(wù)調(diào)用失敗。

在閱讀完高德地圖API服務(wù)條款后,勾選此選項,點擊“提交”,完成 Key 的申請,此時您可以在所創(chuàng)建的應(yīng)用下面看到剛申請的 Key 了。
如何獲取AppID
請在當前應(yīng)用的Ablitiy中使用如下代碼獲取
let flag = bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_SIGNATURE_INFO;
let bundleInfo = bundleManager.getBundleInfoForSelfSync(flag)
let appId = bundleInfo.signatureInfo.appId;
注意事項
- 獲取AppID之前需要先配置應(yīng)用的簽名信息。
- 目前通過DevEco Studio連接云真機獲取到的appId不全,只獲取到了"包名_", 使用云真機調(diào)試高德地圖SDK時會導致鑒權(quán)不通過,正確的appId形式為:"包名_簽名信息", 例如:com.amap.demo_BGtGgVB3ASqU7ar1nHkwX4s0nIexDbEwqNrVoatUDs17GrClWC7V2/zhoYh6tFQHAd5DASWVTEAgvZfzrEGljjs=

獲取定位數(shù)據(jù)
使用定位SDK之前,需要在 config.json 文件中進行相關(guān)權(quán)限設(shè)置,確保定位功能可以正常使用。
第一步,配置module.json5
首先,聲明權(quán)限
...
"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"
}
}
]
...
第二步,向工程中添加定位開發(fā)包
從ohpm倉庫獲取定位包,依次添加依賴。
"dependencies": {
"@amap/amap_lbs_common": "1.0.0",
"@amap/amap_lbs_location": "1.0.0"
}
第三步,初始化定位
首先,設(shè)置Key
[獲取Key]
AMapLocationManagerImpl.setApiKey("您的key");
初始化隱私政策
AMapLocationManagerImpl.updatePrivacyShow(AMapPrivacyShowStatus.DidShow, AMapPrivacyInfoStatus.DidContain, getContext(this))
AMapLocationManagerImpl.updatePrivacyAgree(AMapPrivacyAgreeStatus.DidAgree, getContext(this))
創(chuàng)建AMapLocationManagerImpl
locationManger?: AMapLocationManagerImpl;
this.locationManger = new AMapLocationManagerImpl(this.context);
動態(tài)申請相關(guān)權(quán)限
reqPermissionsFromUser(permissions: Array< Permissions >): void {
let context: Context = getContext(this) as common.UIAbilityContext;
let atManager: abilityAccessCtrl.AtManager = abilityAccessCtrl.createAtManager();
// requestPermissionsFromUser會判斷權(quán)限的授權(quán)狀態(tài)來決定是否喚起彈窗
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) {
// 用戶授權(quán),可以繼續(xù)訪問目標操作
} else {
// 用戶拒絕授權(quán),提示用戶必須授權(quán)才能訪問當前頁面的功能,并引導用戶到系統(tǒng)設(shè)置中打開相應(yīng)的權(quán)限
return;
}
}
// 授權(quán)成功
}).catch((err: BusinessError) = > {
console.error(`Failed to request permissions from user. Code is ${err.code}, message is ${err.message}`);
})
}
最后,開始請求相關(guān)定位信息
如:連續(xù)定位
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()
如需在后臺定位,則要在應(yīng)用切入后臺前調(diào)用開啟后臺定位任務(wù)
this.locationManger?.startContinuousTask()
回到前臺可以停止后臺任務(wù)
this.locationManger?.stopContinuousTask()
至此就可以看到定位相關(guān)信息。也可依據(jù)參考手冊實現(xiàn)單次定位、最后位置等操作。
審核編輯 黃宇
-
定位
+關(guān)注
關(guān)注
5文章
1532瀏覽量
36445 -
SDK
+關(guān)注
關(guān)注
3文章
1092瀏覽量
50946 -
定位數(shù)據(jù)
+關(guān)注
關(guān)注
0文章
3瀏覽量
1069 -
鴻蒙
+關(guān)注
關(guān)注
60文章
2776瀏覽量
45206
發(fā)布評論請先 登錄
鴻蒙地圖功能開發(fā)【3. 代碼開發(fā)】##地圖開發(fā)##
《仿盒馬》app開發(fā)技術(shù)分享-- 定位獲取(25)
DIY --GPS +VB版谷歌地圖定位個人定位黑匣子
【合宙Air551G雙頻定位開發(fā)板試用體驗】 獲取定位數(shù)據(jù)
鴻蒙應(yīng)用開發(fā)的JS UI框架如何實現(xiàn)高德地圖的訪問?
高德地圖要圓夢自動駕駛?
openai api key獲取的三種方案(有一種可以白嫖到 api key)
如何獲取 OpenAI API Key?API 獲取與代碼調(diào)用示例 (詳解教程)
HarmonyOS實戰(zhàn):高德地圖自定義定位圖標展示
HarmonyOS實戰(zhàn):高德地圖定位功能完整流程詳解
谷歌地圖GPS定位
LuatOS AGPS 輔助定位開發(fā)實戰(zhàn)教程

鴻蒙NXET實戰(zhàn):高德地圖定位SDK【獲取Key+獲取定位數(shù)據(jù)】(二)
評論