harmony 鸿蒙Class (GeolocationPermissions)
Class (GeolocationPermissions)
Web组件地理位置权限管理对象。
说明:
本模块接口从API version 9开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
示例效果请以真机运行为准,当前IDE预览器不支持。
目前调用GeolocationPermissions下的方法,都需要先加载Web组件。
需要权限
访问地理位置时需添加权限:ohos.permission.LOCATION、ohos.permission.APPROXIMATELY_LOCATION、ohos.permission.LOCATION_IN_BACKGROUND,具体权限说明请参考位置服务。
allowGeolocation
static allowGeolocation(origin: string, incognito?: boolean): void
允许指定来源使用地理位置接口。
系统能力: SystemCapability.Web.Webview.Core
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
origin | string | 是 | 指定源的字符串索引 |
incognito11+ | boolean | 否 | true表示隐私模式下允许指定来源使用地理位置,false表示正常非隐私模式下允许指定来源使用地理位置。 |
错误码:
以下错误码的详细介绍请参见webview错误码。
错误码ID | 错误信息 |
---|---|
17100011 | Invalid origin. |
401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. |
示例:
// xxx.ets
import { webview } from '@kit.ArkWeb';
import { BusinessError } from '@kit.BasicServicesKit';
@Entry
@Component
struct WebComponent {
controller: webview.WebviewController = new webview.WebviewController();
origin: string = "file:///";
build() {
Column() {
Button('allowGeolocation')
.onClick(() => {
try {
webview.GeolocationPermissions.allowGeolocation(this.origin);
} catch (error) {
console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`);
}
})
Web({ src: 'www.example.com', controller: this.controller })
}
}
}
deleteGeolocation
static deleteGeolocation(origin: string, incognito?: boolean): void
清除指定来源的地理位置权限状态。
系统能力: SystemCapability.Web.Webview.Core
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
origin | string | 是 | 指定源的字符串索引 |
incognito11+ | boolean | 否 | true表示隐私模式下清除指定来源的地理位置权限状态,false表示正常非隐私模式下清除指定来源的地理位置权限状态。 |
错误码:
以下错误码的详细介绍请参见webview错误码。
错误码ID | 错误信息 |
---|---|
17100011 | Invalid origin. |
401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. |
示例:
// xxx.ets
import { webview } from '@kit.ArkWeb';
import { BusinessError } from '@kit.BasicServicesKit';
@Entry
@Component
struct WebComponent {
controller: webview.WebviewController = new webview.WebviewController();
origin: string = "file:///";
build() {
Column() {
Button('deleteGeolocation')
.onClick(() => {
try {
webview.GeolocationPermissions.deleteGeolocation(this.origin);
} catch (error) {
console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`);
}
})
Web({ src: 'www.example.com', controller: this.controller })
}
}
}
getAccessibleGeolocation
static getAccessibleGeolocation(origin: string, callback: AsyncCallback<boolean>, incognito?: boolean): void
以回调方式异步获取指定源的地理位置权限状态。
系统能力: SystemCapability.Web.Webview.Core
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
origin | string | 是 | 指定源的字符串索引 |
callback | AsyncCallback<boolean> | 是 | 返回指定源的地理位置权限状态。 获取成功,true表示已授权,false表示拒绝访问。 获取失败,表示不存在指定源的权限状态。 |
incognito11+ | boolean | 否 | true表示获取隐私模式下以回调方式异步获取指定源的地理位置权限状态,false表示正常非隐私模式下以回调方式异步获取指定源的地理位置权限状态。 |
错误码:
以下错误码的详细介绍请参见webview错误码。
错误码ID | 错误信息 |
---|---|
17100011 | Invalid origin. |
401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. |
示例:
// xxx.ets
import { webview } from '@kit.ArkWeb';
import { BusinessError } from '@kit.BasicServicesKit';
@Entry
@Component
struct WebComponent {
controller: webview.WebviewController = new webview.WebviewController();
origin: string = "file:///";
build() {
Column() {
Button('getAccessibleGeolocation')
.onClick(() => {
try {
webview.GeolocationPermissions.getAccessibleGeolocation(this.origin, (error, result) => {
if (error) {
console.error(`getAccessibleGeolocationAsync error, ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`);
return;
}
console.log('getAccessibleGeolocationAsync result: ' + result);
});
} catch (error) {
console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`);
}
})
Web({ src: 'www.example.com', controller: this.controller })
}
}
}
getAccessibleGeolocation
static getAccessibleGeolocation(origin: string, incognito?: boolean): Promise<boolean>
以Promise方式异步获取指定源的地理位置权限状态。
系统能力: SystemCapability.Web.Webview.Core
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
origin | string | 是 | 指定源的字符串索引。 |
incognito11+ | boolean | 否 | true表示获取隐私模式下以Promise方式异步获取指定源的地理位置权限状态,false表示正常非隐私模式下以Promise方式异步获取指定源的地理位置权限状态。 |
返回值:
类型 | 说明 |
---|---|
Promise<boolean> | Promise实例,用于获取指定源的权限状态。 获取成功,true表示已授权,false表示拒绝访问。 获取失败,表示不存在指定源的权限状态。 |
错误码:
以下错误码的详细介绍请参见webview错误码。
错误码ID | 错误信息 |
---|---|
17100011 | Invalid origin. |
401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. |
示例:
// xxx.ets
import { webview } from '@kit.ArkWeb';
import { BusinessError } from '@kit.BasicServicesKit';
@Entry
@Component
struct WebComponent {
controller: webview.WebviewController = new webview.WebviewController();
origin: string = "file:///";
build() {
Column() {
Button('getAccessibleGeolocation')
.onClick(() => {
try {
webview.GeolocationPermissions.getAccessibleGeolocation(this.origin)
.then(result => {
console.log('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}`);
}
})
Web({ src: 'www.example.com', controller: this.controller })
}
}
}
getStoredGeolocation
static getStoredGeolocation(callback: AsyncCallback<Array<string>>, incognito?: boolean): void
以回调方式异步获取已存储地理位置权限状态的所有源信息。
系统能力: SystemCapability.Web.Webview.Core
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
callback | AsyncCallback<Array<string>> | 是 | 返回已存储地理位置权限状态的所有源信息。 |
incognito11+ | boolean | 否 | true表示获取隐私模式下以回调方式异步获取已存储地理位置权限状态的所有源信息,false表示正常非隐私模式下以回调方式异步获取已存储地理位置权限状态的所有源信息。 |
错误码:
以下错误码的详细介绍请参见webview错误码。
错误码ID | 错误信息 |
---|---|
401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. |
示例:
// xxx.ets
import { webview } from '@kit.ArkWeb';
import { BusinessError } from '@kit.BasicServicesKit';
@Entry
@Component
struct WebComponent {
controller: webview.WebviewController = new webview.WebviewController();
build() {
Column() {
Button('getStoredGeolocation')
.onClick(() => {
try {
webview.GeolocationPermissions.getStoredGeolocation((error, origins) => {
if (error) {
console.error(`getStoredGeolocationAsync error, ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`);
return;
}
let origins_str: string = origins.join();
console.log('getStoredGeolocationAsync origins: ' + origins_str);
});
} catch (error) {
console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`);
}
})
Web({ src: 'www.example.com', controller: this.controller })
}
}
}
getStoredGeolocation
static getStoredGeolocation(incognito?: boolean): Promise<Array<string>>
以Promise方式异步获取已存储地理位置权限状态的所有源信息。
系统能力: SystemCapability.Web.Webview.Core
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
incognito11+ | boolean | 否 | true表示获取隐私模式下以Promise方式异步获取已存储地理位置权限状态的所有源信息,false表示正常非隐私模式下以Promise方式异步获取已存储地理位置权限状态的所有源信息。 |
返回值:
类型 | 说明 |
---|---|
Promise<Array<string>> | Promise实例,用于获取已存储地理位置权限状态的所有源信息。 |
错误码:
以下错误码的详细介绍请参见webview错误码。
错误码ID | 错误信息 |
---|---|
401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. |
示例:
// xxx.ets
import { webview } from '@kit.ArkWeb';
import { BusinessError } from '@kit.BasicServicesKit';
@Entry
@Component
struct WebComponent {
controller: webview.WebviewController = new webview.WebviewController();
build() {
Column() {
Button('getStoredGeolocation')
.onClick(() => {
try {
webview.GeolocationPermissions.getStoredGeolocation()
.then(origins => {
let origins_str: string = origins.join();
console.log('getStoredGeolocationPromise origins: ' + origins_str);
}).catch((error: BusinessError) => {
console.error(`getStoredGeolocationPromise error, ErrorCode: ${error.code}, Message: ${error.message}`);
});
} catch (error) {
console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`);
}
})
Web({ src: 'www.example.com', controller: this.controller })
}
}
}
deleteAllGeolocation
static deleteAllGeolocation(incognito?: boolean): void
清除所有来源的地理位置权限状态。
系统能力: SystemCapability.Web.Webview.Core
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
incognito11+ | boolean | 否 | true表示获取隐私模式下清除所有来源的地理位置权限状态,false表示正常非隐私模式下清除所有来源的地理位置权限状态。 |
示例:
// xxx.ets
import { webview } from '@kit.ArkWeb';
import { BusinessError } from '@kit.BasicServicesKit';
@Entry
@Component
struct WebComponent {
controller: webview.WebviewController = new webview.WebviewController();
build() {
Column() {
Button('deleteAllGeolocation')
.onClick(() => {
try {
webview.GeolocationPermissions.deleteAllGeolocation();
} catch (error) {
console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`);
}
})
Web({ src: 'www.example.com', controller: this.controller })
}
}
}
你可能感兴趣的鸿蒙文章
harmony 鸿蒙ArkWeb_ControllerAPI
harmony 鸿蒙ArkWeb_CookieManagerAPI
harmony 鸿蒙ArkWeb_JavaScriptBridgeData
harmony 鸿蒙ArkWeb_JavaScriptObject
- 所属分类: 后端技术
- 本文标签:
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦