# 管理位置权限 Web组件提供位置权限管理能力。开发者可以通过[onGeolocationShow()](../reference/apis-arkweb/ts-basic-components-web.md#ongeolocationshow)接口对某个网站进行位置权限管理。Web组件根据接口响应结果,决定是否赋予前端页面权限。 - 使用获取设备位置功能前请在module.json5中添加位置相关权限,权限的添加方法请参考[在配置文件中声明权限](../security/AccessToken/declare-permissions.md)。 ``` "requestPermissions":[ { "name" : "ohos.permission.LOCATION" }, { "name" : "ohos.permission.APPROXIMATELY_LOCATION" }, { "name" : "ohos.permission.LOCATION_IN_BACKGROUND" } ] ``` 在下面的示例中,用户点击前端页面"获取位置"按钮,Web组件通过弹窗的形式通知应用侧位置权限请求消息。 - 前端页面代码。 ```html

位置信息

``` - 应用代码。 ```ts // xxx.ets 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)); console.info('data permissions:' + data.permissions); console.info('data authResults:' + data.authResults); }).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); // 不允许此站点地理位置权限请求 } } }) }) } } } ```