HarmonyOS webview如何调用地图定位能力,帮忙给下用法还有示例?

HarmonyOS webview如何调用地图定位能力,帮忙给下用法还有示例?

HarmonyOS
2024-11-06 10:23:11
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zbw_apple

需要手动确定提供地理位置权限,参考链接:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/web-geolocation-permission-0000001820879917-V5

配置如下:

首先在module.json5文件中加上。

"requestPermissions":[  
{ "name": "ohos.permission.INTERNET" },  
{ "name": "ohos.permission.GET_NETWORK_INFO" },  
{ "name": "ohos.permission.LOCATION", "reason": "$string:app_name", "usedScene":  
  { "abilities": [ "FormAbility" ], "when": "inuse" }  
},  
{ "name": "ohos.permission.APPROXIMATELY_LOCATION", "reason": "$string:app_name", "usedScene":  
  { "abilities": [ "FormAbility" ], "when": "inuse" }  
}  
]
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.

参考代码:

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 WebComponent { 
  controller: web_webview.WebviewController = new web_webview.WebviewController(); 
 
  build() { 
    Column() { 
      Web({ 
        src: 'https://xxx-b-fat.huawei.com.cn/xxx/loanWeb/xxxx/amapFrame.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.
分享
微博
QQ
微信
回复
2024-11-06 16:01:01
相关问题
HarmonyOS 地图定位介绍
1018浏览 • 1回复 待解决
HarmonyOS 定位服务、地图服务
709浏览 • 1回复 待解决
HarmonyOS Map地图POI能力
777浏览 • 1回复 待解决
ob有如何报错,麻烦帮忙看一
5885浏览 • 1回复 待解决
startAbility调用失败如何定位
1488浏览 • 1回复 待解决
HarmonyOS地图标记和定位怎么使用
1327浏览 • 1回复 待解决
HarmonyOS 系统定位及语音服务能力
965浏览 • 1回复 待解决
HarmonyOS 地图组件里marker固定位
673浏览 • 1回复 待解决
Webview是否支持定位功能?
1500浏览 • 1回复 待解决
HarmonyOS 定位地图是否有推荐的SDK?
1032浏览 • 1回复 待解决