HarmonyOS鸿蒙Next中webview加载的页面获取定位失败

HarmonyOS鸿蒙Next中webview加载的页面获取定位失败 1、webview 加载网页无法获取定位

2、测试网页地址: 高德 https://m.amap.com , 腾讯地图 https://mapapi.qq.com/web/mapComponents/locationPicker/v/index.html

3、在华为鸿蒙自带的浏览器中也无法获取位置信息

3 回复

这个网址我们试了下web加载出来的位置不准确,但是我们再华为浏览器中打开显示的位置是准确的

请参考如下代码

import { webview } from '@kit.ArkWeb';
import abilityAccessCtrl from '@ohos.abilityAccessCtrl';

@Entry
@Component
struct Index {
  @State message: string = 'Hello World';
  @State url: string = 'https://ywtb.ga.tj.gov.cn/frontend/weixin/';

  controller: webview.WebviewController = new webview.WebviewController();

  onBackPress() {
    if (this.controller.accessStep(-1)) {
      this.controller.backward();
      return true
    } else {
      return false
    }
  }

  aboutToAppear() {
    webview.WebviewController.setWebDebuggingAccess(true);
    let atManager = abilityAccessCtrl.createAtManager();
    atManager.requestPermissionsFromUser(getContext(this), ['ohos.permission.CAMERA',"ohos.permission.MICROPHONE","ohos.permission.LOCATION","ohos.permission.APPROXIMATELY_LOCATION"])
      .then(data => {
        let result: Array<number> = data.authResults;
        let hasPermissions1 = true;
        result.forEach(item => {
          if (item === -1) {
            hasPermissions1 = false;
          }
        })
        if (hasPermissions1) {
          console.info("hasPermissions1");
        } else {
          console.info(" not hasPermissions1");
        }
      }).catch(() => {
      return;
    });
  }

  build() {
    Column(){
      Web({
        src: 'https://mapapi.qq.com/web/mapComponents/locationPicker/v/index.html?search=1&type=0&backurl=https%3A%2F%2Fywtb.ga.tj.gov.cn%2Ffrontend%2Fweixin%2F%23%2Fpages%2FhelpCenter%2FfeedbackOutput&key=XG7BZ-HJFK5-KB5IE-IS4JY-N3A6O-6ZF6G&referer=myapp',
        controller: this.controller
      })
      .width('100%')
      .javaScriptAccess(true)
      .imageAccess(true)
      .onlineImageAccess(true)
      .fileAccess(true)
      .mixedMode(MixedMode.All)
      .layoutWeight(1)
      .domStorageAccess(true)
      .expandSafeArea([SafeAreaType.KEYBOARD])
      .geolocationAccess(true)
      .onGeolocationShow((event) => { // 地理位置权限申请通知
        AlertDialog.show({
          title: '位置权限请求',
          message: '是否允许获取位置信息',
          primaryButton: {
            value: '取消',
            action: () => {
              if (event) {
                event.geolocation.invoke(event.origin, false, false); // 不允许此站点地理位置权限请求
              }
            }
          },
          secondaryButton: {
            value: '确认',
            action: () => {
              if (event) {
                event.geolocation.invoke(event.origin, true, false); // 允许此站点地理位置权限请求
              }
            }
          },
          cancel: () => {
            if (event) {
              event.geolocation.invoke(event.origin, false, false); // 不允许此站点地理位置权限请求
            }
          }
        })
      })
      .onPermissionRequest((event) => {
        if (event) {
          AlertDialog.show({
            title: '提示',
            message: '请求获取设备使用权限',
            primaryButton: {
              value: '拒绝',
              action: () => {
                event.request.deny();
              }
            },
            secondaryButton: {
              value: '确认',
              action: () => {
                event.request.grant(event.request.getAccessibleResource());
              }
            },
            cancel: () => {
              event.request.deny();
            }
          })
        }
      })
    }
    .height('100%')
    .width('100%')
  }
}

更多关于HarmonyOS鸿蒙Next中webview加载的页面获取定位失败的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next中,WebView加载的页面获取定位失败,可能是由于以下原因:

  1. 权限问题:WebView加载的页面需要获取定位权限,但未在应用的配置文件中正确声明或未在运行时动态申请权限。确保在config.json中声明了ohos.permission.LOCATION权限,并在代码中动态请求。

  2. WebView配置问题:WebView默认可能未启用定位功能。需要通过WebView.setWebViewClientWebView.setWebChromeClient进行相关配置,确保定位功能被启用。

  3. 定位服务未开启:设备的定位服务可能未开启,导致WebView无法获取位置信息。确保设备的定位服务已开启。

  4. 网络问题:定位服务依赖网络,若网络连接不稳定或未连接,可能导致定位失败。检查设备的网络连接状态。

  5. WebView版本问题:不同版本的WebView对定位功能的支持可能存在差异,确保使用的WebView版本支持定位功能。

  6. 页面代码问题:页面中的JavaScript代码可能存在问题,导致无法正确获取定位。检查页面代码,确保定位相关的JavaScript逻辑正确。

  7. 系统限制:某些系统设置或安全策略可能限制了WebView的定位功能。检查系统设置,确保未对WebView的定位功能进行限制。

通过排查以上原因,可以解决HarmonyOS鸿蒙Next中WebView加载的页面获取定位失败的问题。

在HarmonyOS鸿蒙Next中,WebView加载的页面获取定位失败,可能是以下原因导致:

  1. 权限问题:确保应用已获取定位权限(ohos.permission.LOCATION),并在config.json中正确配置。

  2. WebView设置:检查WebView是否启用了定位功能,通过WebView.setGeolocationEnabled(true)开启。

  3. 页面代码问题:确保网页代码正确实现了Geolocation API,并处理了可能的错误回调。

  4. 模拟器限制:在模拟器中可能无法获取真实定位,建议在真机上测试。

  5. 网络问题:确保设备网络连接正常,定位服务依赖网络请求。

检查以上问题并逐一排查,通常可以解决定位失败的问题。

回到顶部