前言
地圖定位功能相信很多人都用過,在鴻蒙的應(yīng)用程序開發(fā)中,使用高德地圖的定位功能變得十分常見,那么在鴻蒙中一次完整的地位功能怎么實(shí)現(xiàn)?如果定位失敗了,能否獲取前幾次的定位呢?本篇文章帶你實(shí)現(xiàn)一個(gè)完整的定位功能流程,建議點(diǎn)贊收藏!
需求分析
要想實(shí)現(xiàn)一個(gè)完整的定位需求流程,就必須要做好準(zhǔn)備工作,了解實(shí)現(xiàn)需求的具體步驟。
- 權(quán)限申請(qǐng)
- 檢查 GPS 是否打開
- 單次定位還是多次定位
- 定位失敗處理
技術(shù)實(shí)現(xiàn)
要想實(shí)現(xiàn)一次完整的定位流程,必須根據(jù)需要分析一步步去實(shí)現(xiàn),由于高德地圖的引入太過于簡(jiǎn)單,這里一筆帶過。重點(diǎn)講解完整實(shí)現(xiàn)的步驟。
- 添加基本定位權(quán)限,在 entry 模塊下的 module.json5 中添加定位必要的兩個(gè)權(quán)限。
{
// user_grant
"name": "ohos.permission.APPROXIMATELY_LOCATION",
"reason": "$string:location_permissions_reason",
"usedScene": {
"abilities": [
"EntryAbility"
],
"when": "always"
}
},
{
// user_grant
"name": "ohos.permission.LOCATION",
"reason": "$string:location_permissions_reason",
"usedScene": {
"abilities": [
"EntryAbility"
],
"when": "always"
}
},
- 在頁(yè)面中進(jìn)行權(quán)限申請(qǐng),一般是在 onPageShow 生命周期方法中申請(qǐng),先檢查系統(tǒng) GPS 定位開關(guān)是否開啟,如果沒有開啟則提示用戶跳轉(zhuǎn)到系統(tǒng)指定位置打開。
let location = geoLocationManager.isLocationEnabled()
if (!location) {
let dialog = new OpenSystemGPSEHelper()
dialog.show(this.getUIContext(),getContext(this) as common.UIAbilityContext,()= >{
this.currentCity = "定位失敗"
})
}
//GPS跳轉(zhuǎn)頁(yè)面
context.startAbility(
{
bundleName: "com.huawei.hmos.settings",
abilityName: "com.huawei.hmos.settings.MainAbility",
uri: "location_manager_settings"
},
- 確認(rèn) GPS 打開之后,開始 申請(qǐng) 用戶權(quán)限。
static applyPermission(context: common.UIAbilityContext, permissions: Array< Permissions >, grantedBlock: () = > void,
deniedBlock?: () = > void) {
let atManager = abilityAccessCtrl.createAtManager()
let permissionGrantedNumber: number = 0
atManager.requestPermissionsFromUser(context, permissions).then((data) = > {
for (let index = 0; index < data.authResults.length; index++) {
if (data.authResults[index] == 0) {
permissionGrantedNumber++;
}
}
if (permissionGrantedNumber == permissions.length) {
grantedBlock()
} else {
if (deniedBlock) {
deniedBlock()
} else {
PermissionUtil.openPermissionsInSystemSettings(context)
}
}
})
}
- 如果用戶打開權(quán)限,則直接開始定位服務(wù),否則提示用戶跳轉(zhuǎn)到系統(tǒng)指定位置打開權(quán)限。
let wantInfo: Want = {
bundleName: 'com.huawei.hmos.settings',
abilityName: 'com.huawei.hmos.settings.MainAbility',
uri: 'application_info_entry',
parameters: {
settingsParamBundleName: bundleInfo.name
}
}
context.startAbility(wantInfo).then(() = > {
})
- 確認(rèn)定位權(quán)限沒問題后,開始定位,高德提供的定位有多次和單次,這里使用單次定位。
let listener: IAMapLocationListener = {
onLocationChanged: (location) = > {
console.log("當(dāng)前定位1:"+location.latitude+",---longitude:"+location.longitude)
this.transformCity(location.latitude,location.longitude)
}, onLocationError: (error) = > {
}
};
LocationManager.getInstance().addListener(listener)
LocationManager.getInstance().initLocation()
// 定位參數(shù)配置
let options: AMapLocationOption = {
//定位優(yōu)先配置選項(xiàng)
priority: geoLocationManager.LocationRequestPriority.FIRST_FIX,
//定位場(chǎng)景設(shè)置
scenario: geoLocationManager.LocationRequestScenario.UNSET,
//定位精度 單位:米
maxAccuracy: 0,
//指定單次定位超時(shí)時(shí)間
singleLocationTimeout: 3000,
//定位是否返回逆地理信息
locatingWithReGeocode: true,
//逆地址語(yǔ)言類型
reGeocodeLanguage: AMapLocationReGeocodeLanguage.Chinese,
isOffset: false //是否加偏
}
// 設(shè)置配置
this.locationManger?.setLocationOption(AMapLocationType.Single, options)
if (this.listener != undefined) {
// 監(jiān)聽
this.locationManger?.setLocationListener(AMapLocationType.Single, this.listener)
}
// 啟動(dòng)定位
this.locationManger?.requestSingleLocation()
- 定位成功拿到定位的信息,在實(shí)際開發(fā)中盡管設(shè)置中已經(jīng)設(shè)置返回逆地理信息,但并沒有返回具體信息,這點(diǎn)實(shí)在是無法理解,只能得到當(dāng)前位置的經(jīng)緯度。這個(gè)時(shí)候需要將經(jīng)緯度轉(zhuǎn)換為當(dāng)前位置名稱。
- 在開發(fā)中通過使用華為官方系統(tǒng)中的 api,對(duì)經(jīng)緯度進(jìn)行逆編碼才能獲取準(zhǔn)備的位置名稱。
let reverseGeocodeRequest:geoLocationManager.ReverseGeoCodeRequest = {"latitude":latitude, "longitude":longitude, "maxItems": 1};
try {
geoLocationManager.getAddressesFromLocation(reverseGeocodeRequest, (err, data) = > {})
- 如果地圖定位失敗,則可以根據(jù)高德地圖提供的方法,嘗試拿取歷史定位信息。
LocationManager.getInstance().getLastLocation({
onLocationChanged: (location) = > {
console.info('地圖定位緩存獲取成功: ' + JSON.stringify(location))
if (success) {
success()
}
}, onLocationError: (e) = > {
console.info('地圖定位緩存獲取失敗: ' + JSON.stringify(e))
if (success) {
success()
}
}
})
- 如果歷史定位信息也獲取失敗,這時(shí)就能使用默認(rèn)定位位置了。
總結(jié)
定位功能實(shí)現(xiàn)起來比較簡(jiǎn)單,但是完整的定位流程及細(xì)節(jié)處理才是本篇文章的關(guān)鍵,相信看完本篇文章你已經(jīng)學(xué)會(huì)在鴻蒙中怎么使用高德定位功能了,快去動(dòng)手嘗試一下吧!
審核編輯 黃宇
-
定位
+關(guān)注
關(guān)注
5文章
1443瀏覽量
35971 -
HarmonyOS
+關(guān)注
關(guān)注
80文章
2126瀏覽量
33043
發(fā)布評(píng)論請(qǐng)先 登錄
【HarmonyOS 5】鴻蒙星閃NearLink詳解

是德示波器MSOX3104A自動(dòng)測(cè)量功能詳解與實(shí)戰(zhàn)技巧

高德地圖攜手華為推出長(zhǎng)隧道車道級(jí)導(dǎo)航
鴻蒙地圖功能開發(fā)【3. 代碼開發(fā)】##地圖開發(fā)##
鴻蒙地圖功能開發(fā)【1. 開發(fā)準(zhǔn)備】##地圖開發(fā)##
【HarmonyOS 5】桌面快捷方式功能實(shí)現(xiàn)詳解

【HarmonyOS 5】鴻蒙中的UIAbility詳解(三)
高德發(fā)布智能眼鏡行業(yè)解決方案
谷歌地圖GPS定位
高德地圖與Rokid達(dá)成合作
高質(zhì)量 HarmonyOS 權(quán)限管控流程

高德地圖與長(zhǎng)城汽車共建出行聯(lián)合創(chuàng)新LAB
長(zhǎng)城汽車聯(lián)手高德地圖打造出行聯(lián)合創(chuàng)新LAB
淺談芯片制造的完整流程

評(píng)論