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

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

HarmonyOS
2024-03-17 17:44:12
370浏览
收藏 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); // 不允许此站点地理位置权限请求 
              } 
            } 
          }) 
        }) 
    } 
  } 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.

在“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>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
分享
微博
QQ
微信
回复
2024-03-18 21:20:48
相关问题
定位授权 直接提示用户拒绝授权
1055浏览 • 1回复 待解决
HarmonyOS Web如何区分弹窗类型
493浏览 • 1回复 待解决
Web加载失败问题定位定界指导
790浏览 • 1回复 待解决
HarmonyOS 如何创建自定义的全局弹窗
753浏览 • 1回复 待解决
如何在自定义弹窗再次弹窗
3210浏览 • 1回复 待解决
HarmonyOS 创建全局弹窗的推荐方案
676浏览 • 1回复 待解决
HarmonyOS 关于Web的动态创建
857浏览 • 1回复 待解决
harmonyos如何实现精准的定位功能
215浏览 • 0回复 待解决
鸿蒙next开发如何获取定位服务?
557浏览 • 2回复 待解决