• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Class (GeolocationPermissions)
2<!--Kit: ArkWeb-->
3<!--Subsystem: Web-->
4<!--Owner: @zhang-yinglie-->
5<!--Designer: @handyohos-->
6<!--Tester: @ghiker-->
7<!--Adviser: @HelloCrease-->
8
9Web组件地理位置权限管理对象。
10
11> **说明:**
12>
13> - 本模块首批接口从API version 9开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
14>
15> - 本Class首批接口从API version 9开始支持。
16>
17> - 示例效果请以真机运行为准,当前DevEco Studio预览器不支持。
18>
19> - 目前调用GeolocationPermissions下的方法,都需要先加载Web组件。
20
21## 导入模块
22
23```ts
24import { webview } from '@kit.ArkWeb';
25```
26
27## 需要权限
28
29访问地理位置时需添加权限:ohos.permission.LOCATIONohos.permission.APPROXIMATELY_LOCATIONohos.permission.LOCATION_IN_BACKGROUND,具体权限说明请参考[位置服务](../apis-location-kit/js-apis-geolocation.md)。
30
31## allowGeolocation
32
33static allowGeolocation(origin: string, incognito?: boolean): void
34
35允许指定来源使用地理位置接口。
36
37**系统能力:** SystemCapability.Web.Webview.Core
38
39**参数:**
40
41| 参数名 | 类型   | 必填 | 说明               |
42| ------ | ------ | ---- | ------------------ |
43| origin | string | 是   |指定源的字符串索引 |
44| incognito<sup>11+</sup>    | boolean | 否   | true表示隐私模式下允许指定来源使用地理位置,false表示正常非隐私模式下允许指定来源使用地理位置。 |
45
46**错误码:**
47
48以下错误码的详细介绍请参见[webview错误码](errorcode-webview.md)。
49
50| 错误码ID | 错误信息                                               |
51| -------- | ------------------------------------------------------ |
52| 17100011 | Invalid origin.                             |
53| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. |
54
55**示例:**
56
57```ts
58// xxx.ets
59import { webview } from '@kit.ArkWeb';
60import { BusinessError } from '@kit.BasicServicesKit';
61
62@Entry
63@Component
64struct WebComponent {
65  controller: webview.WebviewController = new webview.WebviewController();
66  origin: string = "file:///";
67
68  build() {
69    Column() {
70      Button('allowGeolocation')
71        .onClick(() => {
72          try {
73            webview.GeolocationPermissions.allowGeolocation(this.origin);
74          } catch (error) {
75            console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
76          }
77        })
78      Web({ src: 'www.example.com', controller: this.controller })
79    }
80  }
81}
82```
83
84## deleteGeolocation
85
86static deleteGeolocation(origin: string, incognito?: boolean): void
87
88清除指定来源的地理位置权限状态。
89
90**系统能力:** SystemCapability.Web.Webview.Core
91
92**参数:**
93
94| 参数名 | 类型   | 必填 | 说明               |
95| ------ | ------ | ---- | ------------------ |
96| origin | string | 是   | 指定源的字符串索引 |
97| incognito<sup>11+</sup>   | boolean | 否   | true表示隐私模式下清除指定来源的地理位置权限状态,false表示正常非隐私模式下清除指定来源的地理位置权限状态。 |
98
99**错误码:**
100
101以下错误码的详细介绍请参见[webview错误码](errorcode-webview.md)。
102
103| 错误码ID | 错误信息                                               |
104| -------- | ------------------------------------------------------ |
105| 17100011 | Invalid origin.                             |
106| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. |
107
108**示例:**
109
110```ts
111// xxx.ets
112import { webview } from '@kit.ArkWeb';
113import { BusinessError } from '@kit.BasicServicesKit';
114
115@Entry
116@Component
117struct WebComponent {
118  controller: webview.WebviewController = new webview.WebviewController();
119  origin: string = "file:///";
120
121  build() {
122    Column() {
123      Button('deleteGeolocation')
124        .onClick(() => {
125          try {
126            webview.GeolocationPermissions.deleteGeolocation(this.origin);
127          } catch (error) {
128            console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
129          }
130        })
131      Web({ src: 'www.example.com', controller: this.controller })
132    }
133  }
134}
135```
136
137## getAccessibleGeolocation
138
139static getAccessibleGeolocation(origin: string, callback: AsyncCallback\<boolean>, incognito?: boolean): void
140
141以回调方式异步获取指定源的地理位置权限状态。
142
143**系统能力:** SystemCapability.Web.Webview.Core
144
145**参数:**
146
147| 参数名   | 类型                   | 必填 | 说明                                                         |
148| -------- | ---------------------- | ---- | ------------------------------------------------------------ |
149| origin   | string                 | 是   | 指定源的字符串索引                                           |
150| callback | AsyncCallback\<boolean> | 是   | 返回指定源的地理位置权限状态。<br>获取成功,true表示已授权,false表示拒绝访问。<br>获取失败,表示不存在指定源的权限状态。 |
151| incognito<sup>11+</sup>    | boolean | 否   | true表示获取隐私模式下以回调方式异步获取指定源的地理位置权限状态,false表示正常非隐私模式下以回调方式异步获取指定源的地理位置权限状态。 |
152
153**错误码:**
154
155以下错误码的详细介绍请参见[webview错误码](errorcode-webview.md)。
156
157| 错误码ID | 错误信息                                               |
158| -------- | ------------------------------------------------------ |
159| 17100011 | Invalid origin.                             |
160| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. |
161
162**示例:**
163
164```ts
165// xxx.ets
166import { webview } from '@kit.ArkWeb';
167import { BusinessError } from '@kit.BasicServicesKit';
168
169@Entry
170@Component
171struct WebComponent {
172  controller: webview.WebviewController = new webview.WebviewController();
173  origin: string = "file:///";
174
175  build() {
176    Column() {
177      Button('getAccessibleGeolocation')
178        .onClick(() => {
179          try {
180            webview.GeolocationPermissions.getAccessibleGeolocation(this.origin, (error, result) => {
181              if (error) {
182                console.error(`getAccessibleGeolocationAsync error, ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
183                return;
184              }
185              console.info('getAccessibleGeolocationAsync result: ' + result);
186            });
187          } catch (error) {
188            console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
189          }
190        })
191      Web({ src: 'www.example.com', controller: this.controller })
192    }
193  }
194}
195```
196
197## getAccessibleGeolocation
198
199static getAccessibleGeolocation(origin: string, incognito?: boolean): Promise\<boolean>
200
201以Promise方式异步获取指定源的地理位置权限状态。
202
203**系统能力:** SystemCapability.Web.Webview.Core
204
205**参数:**
206
207| 参数名 | 类型 | 必填 | 说明             |
208| ------ | -------- | ---- | -------------------- |
209| origin | string   | 是   | 指定源的字符串索引。 |
210| incognito<sup>11+</sup>    | boolean | 否   | true表示获取隐私模式下以Promise方式异步获取指定源的地理位置权限状态,false表示正常非隐私模式下以Promise方式异步获取指定源的地理位置权限状态。 |
211
212**返回值:**
213
214| 类型             | 说明                                                         |
215| ---------------- | ------------------------------------------------------------ |
216| Promise\<boolean> | Promise实例,用于获取指定源的权限状态。<br>获取成功,true表示已授权,false表示拒绝访问。<br>获取失败,表示不存在指定源的权限状态。 |
217
218**错误码:**
219
220以下错误码的详细介绍请参见[webview错误码](errorcode-webview.md)。
221
222| 错误码ID | 错误信息                                               |
223| -------- | ------------------------------------------------------ |
224| 17100011 | Invalid origin.                             |
225| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. |
226
227**示例:**
228
229```ts
230// xxx.ets
231import { webview } from '@kit.ArkWeb';
232import { BusinessError } from '@kit.BasicServicesKit';
233
234@Entry
235@Component
236struct WebComponent {
237  controller: webview.WebviewController = new webview.WebviewController();
238  origin: string = "file:///";
239
240  build() {
241    Column() {
242      Button('getAccessibleGeolocation')
243        .onClick(() => {
244          try {
245            webview.GeolocationPermissions.getAccessibleGeolocation(this.origin)
246              .then(result => {
247                console.info('getAccessibleGeolocationPromise result: ' + result);
248              }).catch((error: BusinessError) => {
249              console.error(`getAccessibleGeolocationPromise error, ErrorCode: ${error.code},  Message: ${error.message}`);
250            });
251          } catch (error) {
252            console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
253          }
254        })
255      Web({ src: 'www.example.com', controller: this.controller })
256    }
257  }
258}
259```
260
261## getStoredGeolocation
262
263static getStoredGeolocation(callback: AsyncCallback\<Array\<string>>, incognito?: boolean): void
264
265以回调方式异步获取已存储地理位置权限状态的所有源信息。
266
267**系统能力:** SystemCapability.Web.Webview.Core
268
269**参数:**
270
271| 参数名   | 类型                         | 必填 | 说明                                     |
272| -------- | ---------------------------- | ---- | ---------------------------------------- |
273| callback | AsyncCallback\<Array\<string>> | 是   | 返回已存储地理位置权限状态的所有源信息。 |
274| incognito<sup>11+</sup>    | boolean | 否   | true表示获取隐私模式下以回调方式异步获取已存储地理位置权限状态的所有源信息,false表示正常非隐私模式下以回调方式异步获取已存储地理位置权限状态的所有源信息。 |
275
276**错误码:**
277
278以下错误码的详细介绍请参见[webview错误码](errorcode-webview.md)。
279
280| 错误码ID | 错误信息                                                |
281| -------- | ------------------------------------------------------ |
282| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. |
283
284**示例:**
285
286```ts
287// xxx.ets
288import { webview } from '@kit.ArkWeb';
289import { BusinessError } from '@kit.BasicServicesKit';
290
291@Entry
292@Component
293struct WebComponent {
294  controller: webview.WebviewController = new webview.WebviewController();
295
296  build() {
297    Column() {
298      Button('getStoredGeolocation')
299        .onClick(() => {
300          try {
301            webview.GeolocationPermissions.getStoredGeolocation((error, origins) => {
302              if (error) {
303                console.error(`getStoredGeolocationAsync error, ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
304                return;
305              }
306              let origins_str: string = origins.join();
307              console.info('getStoredGeolocationAsync origins: ' + origins_str);
308            });
309          } catch (error) {
310            console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
311          }
312        })
313      Web({ src: 'www.example.com', controller: this.controller })
314    }
315  }
316}
317```
318
319## getStoredGeolocation
320
321static getStoredGeolocation(incognito?: boolean): Promise\<Array\<string>>
322
323以Promise方式异步获取已存储地理位置权限状态的所有源信息。
324
325**系统能力:** SystemCapability.Web.Webview.Core
326
327**参数:**
328
329| 参数名   | 类型                         | 必填 | 说明                                     |
330| -------- | ---------------------------- | ---- | ---------------------------------------- |
331| incognito<sup>11+</sup>   | boolean | 否   | true表示获取隐私模式下以Promise方式异步获取已存储地理位置权限状态的所有源信息,false表示正常非隐私模式下以Promise方式异步获取已存储地理位置权限状态的所有源信息。 |
332
333**返回值:**
334
335| 类型                   | 说明                                                      |
336| ---------------------- | --------------------------------------------------------- |
337| Promise\<Array\<string>> | Promise实例,用于获取已存储地理位置权限状态的所有源信息。 |
338
339**错误码:**
340
341以下错误码的详细介绍请参见[webview错误码](errorcode-webview.md)。
342
343| 错误码ID | 错误信息                                                |
344| -------- | ------------------------------------------------------ |
345| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. |
346
347**示例:**
348
349```ts
350// xxx.ets
351import { webview } from '@kit.ArkWeb';
352import { BusinessError } from '@kit.BasicServicesKit';
353
354@Entry
355@Component
356struct WebComponent {
357  controller: webview.WebviewController = new webview.WebviewController();
358
359  build() {
360    Column() {
361      Button('getStoredGeolocation')
362        .onClick(() => {
363          try {
364            webview.GeolocationPermissions.getStoredGeolocation()
365              .then(origins => {
366                let origins_str: string = origins.join();
367                console.info('getStoredGeolocationPromise origins: ' + origins_str);
368              }).catch((error: BusinessError) => {
369              console.error(`getStoredGeolocationPromise error, ErrorCode: ${error.code},  Message: ${error.message}`);
370            });
371          } catch (error) {
372            console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
373          }
374        })
375      Web({ src: 'www.example.com', controller: this.controller })
376    }
377  }
378}
379```
380
381## deleteAllGeolocation
382
383static deleteAllGeolocation(incognito?: boolean): void
384
385清除所有来源的地理位置权限状态。
386
387**系统能力:** SystemCapability.Web.Webview.Core
388
389**参数:**
390
391| 参数名   | 类型                         | 必填 | 说明                                     |
392| -------- | ---------------------------- | ---- | ---------------------------------------- |
393| incognito<sup>11+</sup>    | boolean | 否   | true表示获取隐私模式下清除所有来源的地理位置权限状态,false表示正常非隐私模式下清除所有来源的地理位置权限状态。 |
394
395**示例:**
396
397```ts
398// xxx.ets
399import { webview } from '@kit.ArkWeb';
400import { BusinessError } from '@kit.BasicServicesKit';
401
402@Entry
403@Component
404struct WebComponent {
405  controller: webview.WebviewController = new webview.WebviewController();
406
407  build() {
408    Column() {
409      Button('deleteAllGeolocation')
410        .onClick(() => {
411          try {
412            webview.GeolocationPermissions.deleteAllGeolocation();
413          } catch (error) {
414            console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
415          }
416        })
417      Web({ src: 'www.example.com', controller: this.controller })
418    }
419  }
420}
421```