HarmonyOS APP定位权限开放后,H5无法获取定位

APP定位权限开放后,H5通过window.,navigator.geolocation.getCurrentPosition方法无法获取定位信息。

HarmonyOS
2024-12-23 15:20:29
3.0w浏览
收藏 0
回答 2
回答 2
按赞同
/
按时间
zbw_apple
// xxx.ets
import { webview } from '@kit.ArkWeb';
import { BusinessError } from '@kit.BasicServicesKit';
import { abilityAccessCtrl, common } from '@kit.AbilityKit';

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 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.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
<!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-12-23 19:36:25
wx678f73867f029

这样h5获取的定位不精确,偏移了很多,这是为什么呢


分享
微博
QQ
微信
回复
2025-01-21 18:15:13


相关问题
HarmonyOS h5拉起app,如何获取参数
420浏览 • 1回复 待解决
获取定位权限没有弹框
2437浏览 • 1回复 待解决
安卓app在鸿蒙2.0中无法获取定位信息
15277浏览 • 4回复 待解决
HarmonyOS定位权限问题
1007浏览 • 1回复 待解决
HarmonyOS H5调用APP图库选择图片
534浏览 • 1回复 待解决
HarmonyOS 获取h5的高度和宽度
443浏览 • 1回复 待解决
HarmonyOS h5客服无法上传相册的图片
424浏览 • 1回复 待解决
HarmonyOS h5想要唤起app如何处理
502浏览 • 1回复 待解决
HarmonyOS 申请定位权限失败问题
713浏览 • 1回复 待解决
HarmonyOS 浏览器访问H5,并唤起App
635浏览 • 1回复 待解决
HarmonyOS H5app通信是否有传输上限
304浏览 • 1回复 待解决
场景化控件中无法获取定位信息
2165浏览 • 1回复 待解决