1/* 2 * Copyright (c) 2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import { webview } from '@kit.ArkWeb'; 17import { BusinessError } from '@kit.BasicServicesKit'; 18import { abilityAccessCtrl, common } from '@kit.AbilityKit'; 19 20let context = getContext(this) as common.UIAbilityContext; 21let atManager = abilityAccessCtrl.createAtManager(); 22 23// 向用户请求位置权限设置。 24atManager.requestPermissionsFromUser(context, ['ohos.permission.APPROXIMATELY_LOCATION']).then((data) => { 25 console.info('data:' + JSON.stringify(data)); 26 console.info('data permissions:' + data.permissions); 27 console.info('data authResults:' + data.authResults); 28}).catch((error: BusinessError) => { 29 console.error(`Failed to request permissions from user. Code is ${error.code}, message is ${error.message}`); 30}) 31 32@Entry 33@Component 34struct WebComponent { 35 controller: webview.WebviewController = new webview.WebviewController(); 36 37 build() { 38 Column() { 39 Web({ src: $rawfile('getLocation.html'), controller: this.controller }) 40 .geolocationAccess(true) 41 .onGeolocationShow((event) => { // 地理位置权限申请通知 42 AlertDialog.show({ 43 title: 'Location permission requests', 44 message: 'Whether to allow access to location information', 45 primaryButton: { 46 value: 'cancel', 47 action: () => { 48 if (event) { 49 event.geolocation.invoke(event.origin, false, false); // 不允许此站点地理位置权限请求 50 } 51 } 52 }, 53 secondaryButton: { 54 value: 'ok', 55 action: () => { 56 if (event) { 57 event.geolocation.invoke(event.origin, true, false); // 允许此站点地理位置权限请求 58 } 59 } 60 }, 61 cancel: () => { 62 if (event) { 63 event.geolocation.invoke(event.origin, false, false); // 不允许此站点地理位置权限请求 64 } 65 } 66 }) 67 }) 68 } 69 } 70} 71