Web中如何创建定位授权弹窗

Web中如何创建定位授权弹窗

HarmonyOS
2024-03-17 17:44:12
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
makeer

在Web组件中使用定位能力可参考指南管理位置权限及以下示例代码。

// xxx.ets 
import { webview } from '@kit.ArkWeb'; 
 
@Entry 
@Component 
struct WebComponent { 
  controller: webview.WebviewController = new webview.WebviewController(); 
 
  build() { 
    Column() { 
      Web({ src: $rawfile('getLocation.html'), controller: this.controller }) 
        .geolocationAccess(true) 
        .onGeolocationShow((event) => { // 地理位置权限申请通知 
          AlertDialog.show({ 
            title: '位置权限请求', 
            message: '是否允许获取位置信息', 
            primaryButton: { 
              value: 'cancel', 
              action: () => { 
                if (event) { 
                  event.geolocation.invoke(event.origin, false, false); // 不允许此站点地理位置权限请求 
                } 
              } 
            }, 
            secondaryButton: { 
              value: 'ok', 
              action: () => { 
                if (event) { 
                  event.geolocation.invoke(event.origin, true, false); // 允许此站点地理位置权限请求 
                } 
              } 
            }, 
            cancel: () => { 
              if (event) { 
                event.geolocation.invoke(event.origin, false, false); // 不允许此站点地理位置权限请求 
              } 
            } 
          }) 
        }) 
    } 
  } 
}

在“src/main/resources/rawfile”路径下创建文件getLocation.html:

<!DOCTYPE html> 
<html> 
<body> 
<p id="locationInfo">位置信息</p> 
<button onclick="getLocation()">获取位置</button> 
<script> 
var locationInfo=document.getElementById("locationInfo"); 
function getLocation(){ 
  if (navigator.geolocation) { 
    <!-- 前端页面访问设备地理位置 --> 
    navigator.geolocation.getCurrentPosition(showPosition); 
  } 
} 
function showPosition(position){ 
  locationInfo.innerHTML="Latitude: " + position.coords.latitude + "<br />Longitude: " + position.coords.longitude; 
} 
</script> 
</body> 
</html>
分享
微博
QQ
微信
回复
2024-03-18 21:20:48
相关问题
HarmonyOS 使用定位api无法弹出授权弹窗
1105浏览 • 1回复 待解决
定位授权 直接提示用户拒绝授权
1325浏览 • 1回复 待解决
HarmonyOS Web如何区分弹窗类型
652浏览 • 1回复 待解决
Web加载失败问题定位定界指导
1080浏览 • 1回复 待解决
HarmonyOS 如何创建自定义的全局弹窗
1112浏览 • 1回复 待解决
HarmonyOS 关于Web的动态创建
1047浏览 • 1回复 待解决
HarmonyOS 创建全局弹窗的推荐方案
912浏览 • 1回复 待解决
如何在自定义弹窗再次弹窗
3518浏览 • 1回复 待解决
HarmonyOS web同层创建失败
696浏览 • 1回复 待解决