• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Class (GeolocationPermissions)
2
3Implements a **GeolocationPermissions** object.
4
5> **NOTE**
6>
7> - The initial APIs of this module are supported since API version 9. Updates will be marked with a superscript to indicate their earliest API version.
8>
9> - The initial APIs of this class are supported since API version 9.
10>
11> - You can preview how this component looks on a real device, but not in DevEco Studio Previewer.
12>
13> - You must load the **Web** component before calling the APIs in **GeolocationPermissions**.
14
15## Modules to Import
16
17```ts
18import { webview } from '@kit.ArkWeb';
19```
20
21## Required Permissions
22
23**ohos.permission.LOCATION**, **ohos.permission.APPROXIMATELY_LOCATION**, and **ohos.permission.LOCATION_IN_BACKGROUND**, which are required for accessing the location information. For details about the permissions, see [@ohos.geolocation (Geolocation)](../apis-location-kit/js-apis-geolocation.md).
24
25## allowGeolocation
26
27static allowGeolocation(origin: string, incognito?: boolean): void
28
29Allows the specified origin to use the geolocation information.
30
31**System capability**: SystemCapability.Web.Webview.Core
32
33**Parameters**
34
35| Name| Type  | Mandatory| Description              |
36| ------ | ------ | ---- | ------------------ |
37| origin | string | Yes  |Index of the origin.|
38| incognito<sup>11+</sup>    | boolean | No  | Whether to allow the specified origin to use the geolocation information in incognito mode. The value **true** means to allow the specified origin to use the geolocation information in incognito mode, and **false** means the opposite.|
39
40**Error codes**
41
42For details about the error codes, see [Webview Error Codes](errorcode-webview.md).
43
44| ID| Error Message                                              |
45| -------- | ------------------------------------------------------ |
46| 17100011 | Invalid origin.                             |
47| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. |
48
49**Example**
50
51```ts
52// xxx.ets
53import { webview } from '@kit.ArkWeb';
54import { BusinessError } from '@kit.BasicServicesKit';
55
56@Entry
57@Component
58struct WebComponent {
59  controller: webview.WebviewController = new webview.WebviewController();
60  origin: string = "file:///";
61
62  build() {
63    Column() {
64      Button('allowGeolocation')
65        .onClick(() => {
66          try {
67            webview.GeolocationPermissions.allowGeolocation(this.origin);
68          } catch (error) {
69            console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
70          }
71        })
72      Web({ src: 'www.example.com', controller: this.controller })
73    }
74  }
75}
76```
77
78## deleteGeolocation
79
80static deleteGeolocation(origin: string, incognito?: boolean): void
81
82Clears the geolocation permission status of a specified origin.
83
84**System capability**: SystemCapability.Web.Webview.Core
85
86**Parameters**
87
88| Name| Type  | Mandatory| Description              |
89| ------ | ------ | ---- | ------------------ |
90| origin | string | Yes  | Index of the origin.|
91| incognito<sup>11+</sup>   | boolean | No  | Whether to clear the geolocation permission status of a specified origin in incognito mode. The value **true** means to clear the geolocation permission status of a specified origin in incognito mode, and **false** means the opposite.|
92
93**Error codes**
94
95For details about the error codes, see [Webview Error Codes](errorcode-webview.md).
96
97| ID| Error Message                                              |
98| -------- | ------------------------------------------------------ |
99| 17100011 | Invalid origin.                             |
100| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. |
101
102**Example**
103
104```ts
105// xxx.ets
106import { webview } from '@kit.ArkWeb';
107import { BusinessError } from '@kit.BasicServicesKit';
108
109@Entry
110@Component
111struct WebComponent {
112  controller: webview.WebviewController = new webview.WebviewController();
113  origin: string = "file:///";
114
115  build() {
116    Column() {
117      Button('deleteGeolocation')
118        .onClick(() => {
119          try {
120            webview.GeolocationPermissions.deleteGeolocation(this.origin);
121          } catch (error) {
122            console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
123          }
124        })
125      Web({ src: 'www.example.com', controller: this.controller })
126    }
127  }
128}
129```
130
131## getAccessibleGeolocation
132
133static getAccessibleGeolocation(origin: string, callback: AsyncCallback\<boolean>, incognito?: boolean): void
134
135Obtains the geolocation permission status of the specified origin. This API uses an asynchronous callback to return the result.
136
137**System capability**: SystemCapability.Web.Webview.Core
138
139**Parameters**
140
141| Name  | Type                  | Mandatory| Description                                                        |
142| -------- | ---------------------- | ---- | ------------------------------------------------------------ |
143| origin   | string                 | Yes  | Index of the origin.                                          |
144| callback | AsyncCallback\<boolean> | Yes  | Callback used to return the geolocation permission status of the specified origin.<br>If the operation is successful, the value **true** means that the geolocation permission is granted, and **false** means the opposite.<br>If the operation fails, the geolocation permission status of the specified origin is not found.|
145| incognito<sup>11+</sup>    | boolean | No  | Whether to obtain the geolocation permission status of the specified origin in incognito mode. The value **true** means to obtain the geolocation permission status of the specified origin in incognito mode, and **false** means the opposite.|
146
147**Error codes**
148
149For details about the error codes, see [Webview Error Codes](errorcode-webview.md).
150
151| ID| Error Message                                              |
152| -------- | ------------------------------------------------------ |
153| 17100011 | Invalid origin.                             |
154| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. |
155
156**Example**
157
158```ts
159// xxx.ets
160import { webview } from '@kit.ArkWeb';
161import { BusinessError } from '@kit.BasicServicesKit';
162
163@Entry
164@Component
165struct WebComponent {
166  controller: webview.WebviewController = new webview.WebviewController();
167  origin: string = "file:///";
168
169  build() {
170    Column() {
171      Button('getAccessibleGeolocation')
172        .onClick(() => {
173          try {
174            webview.GeolocationPermissions.getAccessibleGeolocation(this.origin, (error, result) => {
175              if (error) {
176                console.error(`getAccessibleGeolocationAsync error, ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
177                return;
178              }
179              console.log('getAccessibleGeolocationAsync result: ' + result);
180            });
181          } catch (error) {
182            console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
183          }
184        })
185      Web({ src: 'www.example.com', controller: this.controller })
186    }
187  }
188}
189```
190
191## getAccessibleGeolocation
192
193static getAccessibleGeolocation(origin: string, incognito?: boolean): Promise\<boolean>
194
195Obtains the geolocation permission status of the specified origin. This API uses a promise to return the result.
196
197**System capability**: SystemCapability.Web.Webview.Core
198
199**Parameters**
200
201| Name| Type| Mandatory| Description            |
202| ------ | -------- | ---- | -------------------- |
203| origin | string   | Yes  | Index of the origin.|
204| incognito<sup>11+</sup>    | boolean | No  | Whether to obtain the geolocation permission status of the specified origin in incognito mode. The value **true** means to obtain the geolocation permission status of the specified origin in incognito mode, and **false** means the opposite.|
205
206**Return value**
207
208| Type            | Description                                                        |
209| ---------------- | ------------------------------------------------------------ |
210| Promise\<boolean> | Promise used to return the geolocation permission status of the specified origin.<br>If the operation is successful, the value **true** means that the geolocation permission is granted, and **false** means the opposite.<br>If the operation fails, the geolocation permission status of the specified origin is not found.|
211
212**Error codes**
213
214For details about the error codes, see [Webview Error Codes](errorcode-webview.md).
215
216| ID| Error Message                                              |
217| -------- | ------------------------------------------------------ |
218| 17100011 | Invalid origin.                             |
219| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. |
220
221**Example**
222
223```ts
224// xxx.ets
225import { webview } from '@kit.ArkWeb';
226import { BusinessError } from '@kit.BasicServicesKit';
227
228@Entry
229@Component
230struct WebComponent {
231  controller: webview.WebviewController = new webview.WebviewController();
232  origin: string = "file:///";
233
234  build() {
235    Column() {
236      Button('getAccessibleGeolocation')
237        .onClick(() => {
238          try {
239            webview.GeolocationPermissions.getAccessibleGeolocation(this.origin)
240              .then(result => {
241                console.log('getAccessibleGeolocationPromise result: ' + result);
242              }).catch((error: BusinessError) => {
243              console.error(`getAccessibleGeolocationPromise error, ErrorCode: ${error.code},  Message: ${error.message}`);
244            });
245          } catch (error) {
246            console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
247          }
248        })
249      Web({ src: 'www.example.com', controller: this.controller })
250    }
251  }
252}
253```
254
255## getStoredGeolocation
256
257static getStoredGeolocation(callback: AsyncCallback\<Array\<string>>, incognito?: boolean): void
258
259Obtains the geolocation permission status of all origins. This API uses an asynchronous callback to return the result.
260
261**System capability**: SystemCapability.Web.Webview.Core
262
263**Parameters**
264
265| Name  | Type                        | Mandatory| Description                                    |
266| -------- | ---------------------------- | ---- | ---------------------------------------- |
267| callback | AsyncCallback\<Array\<string>> | Yes  | Callback used to return the geolocation permission status of all origins.|
268| incognito<sup>11+</sup>    | boolean | No  | Whether to obtain the geolocation permission status of all origins in incognito mode. The value **true** means to obtain the geolocation permission status of all origins in incognito mode, and **false** means the opposite.|
269
270**Error codes**
271
272For details about the error codes, see [Webview Error Codes](errorcode-webview.md).
273
274| ID| Error Message                                               |
275| -------- | ------------------------------------------------------ |
276| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. |
277
278**Example**
279
280```ts
281// xxx.ets
282import { webview } from '@kit.ArkWeb';
283import { BusinessError } from '@kit.BasicServicesKit';
284
285@Entry
286@Component
287struct WebComponent {
288  controller: webview.WebviewController = new webview.WebviewController();
289
290  build() {
291    Column() {
292      Button('getStoredGeolocation')
293        .onClick(() => {
294          try {
295            webview.GeolocationPermissions.getStoredGeolocation((error, origins) => {
296              if (error) {
297                console.error(`getStoredGeolocationAsync error, ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
298                return;
299              }
300              let origins_str: string = origins.join();
301              console.log('getStoredGeolocationAsync origins: ' + origins_str);
302            });
303          } catch (error) {
304            console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
305          }
306        })
307      Web({ src: 'www.example.com', controller: this.controller })
308    }
309  }
310}
311```
312
313## getStoredGeolocation
314
315static getStoredGeolocation(incognito?: boolean): Promise\<Array\<string>>
316
317Obtains the geolocation permission status of all origins. This API uses a promise to return the result.
318
319**System capability**: SystemCapability.Web.Webview.Core
320
321**Parameters**
322
323| Name  | Type                        | Mandatory| Description                                    |
324| -------- | ---------------------------- | ---- | ---------------------------------------- |
325| incognito<sup>11+</sup>   | boolean | No  | Whether to obtain the geolocation permission status of all origins in incognito mode. The value **true** means to obtain the geolocation permission status of all origins in incognito mode, and **false** means the opposite.|
326
327**Return value**
328
329| Type                  | Description                                                     |
330| ---------------------- | --------------------------------------------------------- |
331| Promise\<Array\<string>> | Promise used to return the geolocation permission status of all origins.|
332
333**Error codes**
334
335For details about the error codes, see [Webview Error Codes](errorcode-webview.md).
336
337| ID| Error Message                                               |
338| -------- | ------------------------------------------------------ |
339| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. |
340
341**Example**
342
343```ts
344// xxx.ets
345import { webview } from '@kit.ArkWeb';
346import { BusinessError } from '@kit.BasicServicesKit';
347
348@Entry
349@Component
350struct WebComponent {
351  controller: webview.WebviewController = new webview.WebviewController();
352
353  build() {
354    Column() {
355      Button('getStoredGeolocation')
356        .onClick(() => {
357          try {
358            webview.GeolocationPermissions.getStoredGeolocation()
359              .then(origins => {
360                let origins_str: string = origins.join();
361                console.log('getStoredGeolocationPromise origins: ' + origins_str);
362              }).catch((error: BusinessError) => {
363              console.error(`getStoredGeolocationPromise error, ErrorCode: ${error.code},  Message: ${error.message}`);
364            });
365          } catch (error) {
366            console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
367          }
368        })
369      Web({ src: 'www.example.com', controller: this.controller })
370    }
371  }
372}
373```
374
375## deleteAllGeolocation
376
377static deleteAllGeolocation(incognito?: boolean): void
378
379Clears the geolocation permission status of all sources.
380
381**System capability**: SystemCapability.Web.Webview.Core
382
383**Parameters**
384
385| Name  | Type                        | Mandatory| Description                                    |
386| -------- | ---------------------------- | ---- | ---------------------------------------- |
387| incognito<sup>11+</sup>    | boolean | No  | Whether to clear the geolocation permission status of all sources in incognito mode. The value **true** means to clear the geolocation permission status of all sources in incognito mode, and **false** means the opposite.|
388
389**Example**
390
391```ts
392// xxx.ets
393import { webview } from '@kit.ArkWeb';
394import { BusinessError } from '@kit.BasicServicesKit';
395
396@Entry
397@Component
398struct WebComponent {
399  controller: webview.WebviewController = new webview.WebviewController();
400
401  build() {
402    Column() {
403      Button('deleteAllGeolocation')
404        .onClick(() => {
405          try {
406            webview.GeolocationPermissions.deleteAllGeolocation();
407          } catch (error) {
408            console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
409          }
410        })
411      Web({ src: 'www.example.com', controller: this.controller })
412    }
413  }
414}
415```
416