#鸿蒙通关秘籍#如何在鸿蒙开发中正确处理位置权限?

HarmonyOS
2024-12-17 09:43:13
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
墨s诗篇NLP

可以通过onGeolocationShow()接口处理位置权限请求: bash import { webview } from '@kit.ArkWeb'; import { BusinessError } from '@kit.BasicServicesKit'; import { abilityAccessCtrl, common } from '@kit.AbilityKit';

let context = getContext(this) as common.UIAbilityContext; let atManager = abilityAccessCtrl.createAtManager();

// 向用户请求位置权限设置 atManager.requestPermissionsFromUser(context, ["ohos.permission.APPROXIMATELY_LOCATION"]).then((data) => { console.info('data:' + JSON.stringify(data)); }).catch((error: BusinessError) => { console.error(Failed to request permissions from user. Code is ${error.code}, message is ${error.message}); })

@Entry @Component struct WebComponent { controller: webview.WebviewController = new webview.WebviewController();

build() { Column() { Web({ src: $rawfile('getLocation.html'), controller: this.controller }) .geolocationAccess(true) .onGeolocationShow((event) => { AlertDialog.show({ title: '位置权限请求', message: '是否允许获取位置信息', primaryButton: { value: 'cancel', action: () => { if (event) { event.geolocation.invoke(event.origin, false, false); } } }, secondaryButton: { value: 'ok', action: () => { if (event) { event.geolocation.invoke(event.origin, true, false); } } }, cancel: () => { if (event) { event.geolocation.invoke(event.origin, false, false); } } }) }) } } }

分享
微博
QQ
微信
回复
2024-12-17 12:29:40
相关问题