前言
地圖定位功能基本上已經(jīng)成了日常應(yīng)用程序的必備功能之一,在日常開發(fā)地圖定位的功能的時(shí)候難免會(huì)遇到很多意想不到的問題,本篇文章記錄日常開發(fā)過程中的細(xì)節(jié)與完整的流程,幫助更多的開發(fā)者避免遇到類似的問題,建議點(diǎn)贊收藏!
實(shí)現(xiàn)效果
需求分析
- 首先需要實(shí)現(xiàn)一個(gè)自定義的圖標(biāo)替代系統(tǒng)默認(rèn)的箭頭。
- 獲取定位權(quán)限與位置信息。
- 獲取定位結(jié)果并展示當(dāng)前位置。
技術(shù)實(shí)現(xiàn)
- 在鴻蒙的實(shí)際開發(fā)過程中,地圖定位權(quán)限首先需要申請(qǐng)兩個(gè)權(quán)限,分別是:
const permissions: Array< Permissions > = [
'ohos.permission.APPROXIMATELY_LOCATION',
'ohos.permission.LOCATION'
]
static applyPermission(context: common.UIAbilityContext, permissions: Array< Permissions >, grantedBlock: () = > void,
deniedBlock?: () = > void) {
let atManager = abilityAccessCtrl.createAtManager()
let permissionGrantedNumber: number = 0 //記錄已經(jīng)授權(quán)的總個(gè)數(shù)
atManager.requestPermissionsFromUser(context, permissions).then((data) = > {
for (let index = 0; index < data.authResults.length; index++) {
if (data.authResults[index] == 0) { //已授權(quán)
permissionGrantedNumber++;
}
}
if (permissionGrantedNumber == permissions.length) {
grantedBlock()
} else {
if (deniedBlock) {
deniedBlock()
} else {
//打開系統(tǒng)設(shè)置
PermissionUtil.openPermissionsInSystemSettings(context)
}
}
})
}
2. 兩個(gè)權(quán)限必須同時(shí)申請(qǐng),缺一不可,同時(shí)如果權(quán)限是被拒絕過的,那就要手動(dòng)打開系統(tǒng)設(shè)置,跳轉(zhuǎn)到對(duì)應(yīng)應(yīng)用程序的位置提示用戶手動(dòng)打開權(quán)限。
let bundleInfo: bundleManager.BundleInfo =
await bundleManager.getBundleInfoForSelf(bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION);
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(() = > {
})
- 得到系統(tǒng)授權(quán)后,開始使用高德定位。
let listener: IAMapLocationListener = {
onLocationChanged: (location) = > {
console.info('地圖定位成功: ')
}, onLocationError: (e) = > {
console.info('地圖定位失敗: ' + JSON.stringify(e))
if (!this.hasUserLocation) {
// 嘗試獲取緩存位置
this.getLastLocation(success, error)
}
}
};
LocationManager.getInstance().addListener(listener)
- 定位成功后,添加用戶自定義的圖標(biāo)。
this.aMap?.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(userLat,userLon), 15));
let options: MarkerOptions = new MarkerOptions();
options.setPosition(new LatLng(userLat, userLon));
options.setIcon(await BitmapDescriptorFactory.fromView(() = > {
this.customMarkerBuilder()
}))
this.aMap?.addMarker(options);
//自定義圖標(biāo)
@Builder
customMarkerBuilder(){
Image($r("app.media.user_location_icon"))
.width($r('app.float.vp_40'))
.height($r('app.float.vp_40'))
}
- 這里需要特別注意經(jīng)緯度 userLat,userLon 必須是 float 類型,不然定位不準(zhǔn)。這點(diǎn)一定要注意。因?yàn)轼櫭蓻]有提供 float 類型的屬性,這里需要使用 Number.parseFloat 對(duì)數(shù)據(jù)進(jìn)行轉(zhuǎn)換。(真實(shí)慘痛經(jīng)歷,定位跑到歐洲去了)。
- 當(dāng)完成這些操作,大多數(shù)人都認(rèn)為基本上可以正常顯示了,但是萬萬沒想到地圖依然沒有定位到當(dāng)前位置。這是因?yàn)楹芏嗳撕雎粤讼到y(tǒng)的 GPS 定位按鈕是否打開,也就是系統(tǒng)下拉菜單中的位置圖標(biāo)。
- 必須在檢查權(quán)限的時(shí)候,檢查系統(tǒng)位置開關(guān)是否打開。
let location = geoLocationManager.isLocationEnabled()
console.log("定位權(quán)限是否開啟:"+location)
- 如果沒有打開,則需要跳轉(zhuǎn)到系統(tǒng)對(duì)應(yīng)的位置提示用戶打開。
context.startAbility(
{
bundleName: "com.huawei.hmos.settings",
abilityName: "com.huawei.hmos.settings.MainAbility",
uri: "location_manager_settings"
},
- 完成以上操作,地圖就能正常顯示自定義位置圖標(biāo)了。
總結(jié)
鴻蒙對(duì)于位置權(quán)限要求十分嚴(yán)格,必須同時(shí)滿足兩個(gè)權(quán)限申請(qǐng),同時(shí)也要注意 GPS 的位置開關(guān)是否正常打開。另外特別注意的是經(jīng)緯度是否是 float 類型,否則會(huì)導(dǎo)致位置跑偏。學(xué)會(huì)的小伙伴趕緊動(dòng)手試試吧!
審核編輯 黃宇
-
鴻蒙
+關(guān)注
關(guān)注
60文章
2620瀏覽量
44066 -
HarmonyOS
+關(guān)注
關(guān)注
80文章
2126瀏覽量
33110
發(fā)布評(píng)論請(qǐng)先 登錄
HarmonyOS實(shí)戰(zhàn):3秒實(shí)現(xiàn)一個(gè)自定義輪播圖
KiCad 中的自定義規(guī)則(KiCon 演講)

HarmonyOS實(shí)戰(zhàn):自定義時(shí)間選擇器

HarmonyOS實(shí)戰(zhàn):高德地圖定位功能完整流程詳解
HarmonyOS應(yīng)用自定義鍵盤解決方案
如何添加自定義單板
如何快速創(chuàng)建用戶自定義Board和App工程

Altium Designer 15.0自定義元件設(shè)計(jì)

think-cell:自定義think-cell(四)

think-cell;自定義think-cell(一)

美國硅谷高防服務(wù)器自定義解析
創(chuàng)建自定義的基于閃存的引導(dǎo)加載程序(BSL)

評(píng)論