Web中网页如何申请位置权限?

Web中网页如何申请位置权限?

HarmonyOS
2024-08-06 18:32:52
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
fanyu0803

注意:配置文件中别忘记配置权限ohos.permission.LOCATION,ohos.permission.APPROXIMATELY_LOCATION

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.

arkTS代码:

import web_webview from '@ohos.web.webview';
import { abilityAccessCtrl, common } from '@kit.AbilityKit';
import { BusinessError } from '@ohos.base';

let context = getContext(this) as common.UIAbilityContext;
let atManager = abilityAccessCtrl.createAtManager();
// 向用户请求位置权限设置。
atManager.requestPermissionsFromUser(context, ["ohos.permission.APPROXIMATELY_LOCATION"]).then((data) => {
  console.info('data:' + JSON.stringify(data));
  console.info('data permissions:' + data.permissions);
  console.info('data authResults:' + data.authResults);
}).catch((error: BusinessError) => {
  console.error(`Failed to request permissions from user. Code is ${error.code}, message is ${error.message}`);
})
@Entry
@Component
struct Index {
  controller: web_webview.WebviewController = new web_webview.WebviewController()
  dialogController: CustomDialogController | null = null
  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);   // 不允许此站点地理位置权限请求
              }
            }
          })
        })
    }
    .width('100%')
    .height('100%')
    .justifyContent(FlexAlign.Center)
  }
}
  • 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.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
分享
微博
QQ
微信
回复
2024-08-06 20:09:59
相关问题
Web组件如何申请位置权限
1369浏览 • 1回复 待解决
app如何申请位置权限
1382浏览 • 1回复 待解决
HarmonyOS 位置权限申请
1006浏览 • 1回复 待解决
HarmonyOS申请用户位置权限问题
1324浏览 • 1回复 待解决
如何申请广告跟踪权限
1351浏览 • 1回复 待解决
如何申请组合授权权限
1245浏览 • 1回复 待解决
HarmonyOS 动态申请权限申请不了
874浏览 • 1回复 待解决
HarmonyOS Web加载网页白屏
1372浏览 • 1回复 待解决
关于 web 网页打开速度
834浏览 • 1回复 待解决
求告知如何申请广告跟踪权限
1253浏览 • 1回复 待解决
HarmonyOS权限申请问题
1241浏览 • 1回复 待解决