目录

Managing Location Permissions

Since API version 9, the Web component supports the GeolocationPermissions class and onGeolocationShow method for managing web page location permissions. For details, see Application Data Security.

The Web component determines whether to grant the frontend page permission based on the response of the GeolocationPermissions class and onGeolocationShow method. Users can grant applications access to their location for services such as navigation and weather forecasts.

Required Permissions

To obtain the location, you need to configure the location permission in module.json5. For details, see Declaring Permissions in the Configuration File.

"requestPermissions":[
   {
     "name" : "ohos.permission.LOCATION" // Precise location
   },
   {
     "name" : "ohos.permission.APPROXIMATELY_LOCATION" // Approximate location
   },
   {
     "name" : "ohos.permission.LOCATION_IN_BACKGROUND" // Background location
   }
 ]

Requesting Location Permissions

In the following example, when a user clicks the Get Location button on the frontend page, the Web component notifies the application of the location permission request in a dialog box.

Managing Location Permissions

You can manage the location permissions of web pages through the GeolocationPermissions class of the Web component. This class provides methods for adding (allowGeolocation), viewing (getAccessibleGeolocation), and deleting (deleteGeolocation) the location permissions of web pages. For example, you can check whether a web page has applied for the location permission and delete the location permission that a web page has applied for.

import { webview } from '@kit.ArkWeb';
import { BusinessError } from '@kit.BasicServicesKit';

@Entry
@Component
struct WebComponent {
  controller: webview.WebviewController = new webview.WebviewController();
  origin: string = "www.example.com";

  build() {
    Column() {
      // Add the location permission for the specified source so that the onGeolocationShow callback is not triggered when location information is obtained again.
      Button('allowGeolocation')
        .onClick(() => {
          try {
            webview.GeolocationPermissions.allowGeolocation(this.origin);
          } catch (error) {
            console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
          }
        })

      // Delete the location permission for the specified source so that the onGeolocationShow callback is triggered when location information is obtained again.
      Button('deleteGeolocation')
        .onClick(() => {
          try {
            webview.GeolocationPermissions.deleteGeolocation(this.origin);
          } catch (error) {
            console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
          }
        })

      // Query the location permission of the specified source.
      Button('getAccessibleGeolocation')
        .onClick(() => {
          try {
            webview.GeolocationPermissions.getAccessibleGeolocation(this.origin)
              .then(result => {
                console.info('getAccessibleGeolocationPromise result: ' + result);
              }).catch((error: BusinessError) => {
              console.error(`getAccessibleGeolocationPromise error, ErrorCode: ${error.code},  Message: ${error.message}`);
            });
          } catch (error) {
            console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
          }
        })

      // Add the network permission ohos.permission.INTERNET.
      Web({ src: 'www.example.com', controller: this.controller })
    }
  }
}