鸿蒙ArkTS怎样接入舆图【已解决】

打印 上一主题 下一主题

主题 1048|帖子 1048|积分 3144

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
题目描述

用鸿蒙ArkTS语言开辟舆图APP应用时,开辟出现瓶颈。经过调研,很多舆图厂商只接入了鸿蒙Java,就算是有ArkTS版功能也比力少(我的调研不一定全面哈),而自己手动写舆图也很麻烦。

解决方法

使用鸿蒙的Web组件,将HTML页面嵌入到鸿蒙APP中。
简朴来说就是,各大舆图厂商是适配普通Web页面的开辟的,那么就用HTML去使用舆图接口,鸿蒙 ArkTS 作为壳子。
这个方法可以为很多目前由于鸿蒙不完善而堵塞开辟的题目提供一个新思路。
1. 编写HTML(以高德舆图为例)

该HTML的功能:1. 创建舆图,2. 批量添加标记,3. 点击标记显示信息
可以继承拓展功能......
  1. <!DOCTYPE html>
  2. <html>
  3.   <head>
  4.     <meta charset="utf-8" />
  5.     <meta
  6.       name="viewport"
  7.       content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"
  8.     />
  9.     <title>xxxx</title>
  10.     <style>
  11.       html,
  12.       body,
  13.       #container {
  14.         width: 100%;
  15.         height: 100%;
  16.         margin: 0;
  17.         padding: 0;
  18.       }
  19.     </style>
  20.   </head>
  21.   <body style="position: absolute">
  22.     <div
  23.       id="mapContainer"
  24.       style="width: 100%; height: 100vh; border: 1px solid #000000"
  25.     ></div>
  26.     <script type="text/javascript">
  27.       window._AMapSecurityConfig = {
  28.         securityJsCode: "xxx",
  29.       };
  30.     </script>
  31.     <script src="https://webapi.amap.com/loader.js"></script>
  32.     <script type="text/javascript">
  33.       var AMapStore = {
  34.         AMapBox: "", // 高德地图容器
  35.         map: {},
  36.         marker: [], // 标记
  37.       };
  38.       // 存储地点信息
  39.       const locations = [];
  40.       // 加载高德地图容器
  41.       async function LoaderMap() {
  42.         try {
  43.           const AMap = await AMapLoader.load({
  44.             key: "xxxx", // 请替换成你申请的应用key
  45.             version: "2.0", // 指定要加载的 JS API 的版本
  46.           });
  47.           AMapStore.AMapBox = AMap;
  48.         } catch (e) {
  49.           console.error(e);
  50.         }
  51.       }
  52.       // 建立地图
  53.       async function createMap() {
  54.         AMapStore.map = await new AMapStore.AMapBox.Map("mapContainer", {
  55.           viewMode: "2D",
  56.           zoom: 15.4,
  57.           center: [xx, xx],
  58.         });
  59.       }
  60.       // 标记所有地点
  61.       function AllMapMaker() {
  62.         for (let i = 0; i < locations.length; i += 1) {
  63.           MapMaker(locations[i].coordinates, i, locations[i].name);
  64.         }
  65.       }
  66.       // 标记地图
  67.       function MapMaker(positionNum, num, name) {
  68.         AMapStore.marker.push(
  69.           new AMapStore.AMapBox.Marker({
  70.             icon: "https://webapi.amap.com/theme/v1.3/markers/n/mark_b.png",
  71.             position: [positionNum[0], positionNum[1]],
  72.             title: name,
  73.             offset: new AMapStore.AMapBox.Pixel(-13, -30),
  74.           })
  75.         );
  76.         let infoWindow; // 声明 infoWindow 变量以便在点击地图时进行访问
  77.         // 添加标记点击事件监听器
  78.         AMapStore.marker[num].on("click", () => {
  79.           // 如果之前有打开的信息窗体,则先关闭
  80.           if (infoWindow) {
  81.             infoWindow.close();
  82.           }
  83.           // 创建信息窗体
  84.           infoWindow = new AMapStore.AMapBox.InfoWindow({
  85.             content: `<div style="max-width: 300px;">${locations[num].details}</div>`,
  86.             offset: new AMapStore.AMapBox.Pixel(40.15, -42),
  87.             zIndex: 1000,
  88.           });
  89.           // 打开信息窗体
  90.           infoWindow.open(AMapStore.map, AMapStore.marker[num].getPosition());
  91.         });
  92.         // 添加地图点击事件监听器
  93.         AMapStore.map.on("click", () => {
  94.           // 关闭信息窗体
  95.           if (infoWindow) {
  96.             infoWindow.close();
  97.           }
  98.         });
  99.         AMapStore.map.add(AMapStore.marker[num]);
  100.       }
  101.       // 初始化一切
  102.       async function init() {
  103.         await LoaderMap();
  104.         await createMap();
  105.         await AllMapMaker();
  106.       }
  107.       init();
  108.     </script>
  109.   </body>
  110. </html>
复制代码
2. 安置HTML文件

在resources下创建rawfile文件夹,将html文件放入即可

3. 接入鸿蒙ArkTS

  1. import web_webview from '@ohos.web.webview';
  2. @Entry
  3. @Component
  4. struct WebComponent {
  5.   webviewController: web_webview.WebviewController = new web_webview.WebviewController();
  6.   build() {
  7.     Column() {
  8.       // 组件创建时,通过$rawfile加载本地文件map.html
  9.       Web({ src: $rawfile("map.html"), controller: this.webviewController })
  10.     }
  11.   }
  12. }
复制代码
4. 在模拟机或真机运行

   在鸿蒙自带的预览器运行会报错
  舆图加载过程,涉及网络资源获取,必要设置ohos.permission.INTERNET网络访问权限。
以ArkTS的Stage模型为例:
必要在module.json5设置文件中声明权限,添加下列代码即可
  1. {
  2.   "module": {
  3.     ......
  4.     "requestPermissions":[
  5.       {
  6.         "name": "ohos.permission.INTERNET"
  7.       }
  8.     ],
  9.     ......
  10.   }
  11. }
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

傲渊山岳

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表