鸿蒙Flutter实战:19-Flutter集成高德地图,跳转页面方式

前言

在之前的文章现有Flutter项目支持鸿蒙II中,介绍了如何使用第三方插件,同时给出了非常多的使用案例,如

flutter_inappwebview,video_player, image_picker 等,本文将开始介绍如何集成高德地图。

整体方案

通过 MethodChannel 进行消息通信,在 Dart 侧调用原生API,在 ArkTS 侧收到相关调用后,根据参数跳转到指定页面

Dart 侧

dart 复制代码
  static Future<dynamic> redirectNative(String url) {
    return _methodChannel.invokeMethod("redirectNative", {
      "url": url,
    });
  }

ArkTS 侧

ohos/entry/src/main/ets/entryability 创建 OhosPlugin.ets 文件,这里收到到消息后,调用 router.pushUrl 方法跳转到指定页面

ts 复制代码
export default class OhosPlugin implements FlutterPlugin {
  ...
  onAttachedToEngine(binding: FlutterPluginBinding): void {
    this.channel.setMethodCallHandler({
      onMethodCall : (call: MethodCall, result: MethodResult) => {
        switch (call.method) {
          case "redirectNative":
            let url = String(call.argument("url"));
            router.pushUrl({ url: url})
            break;
          default:
            result.notImplemented();
            break;
        }
      }
    })
  }
}

插件写好后,需要在 EntryAbility 中注册:

ts 复制代码
this.addPlugin(new OhosPlugin())

添加原生页面,回到 DevEco,在 pages 目录右键,创建一个空页面, 命名为 Amap

ohos/entry/oh-package.json 文件中引入高德地图SDK:

json 复制代码
  "dependencies": {
    "@amap/amap_lbs_common": ">=1.1.0",
    "@amap/amap_lbs_map3d": ">=2.1.1",
    ...
  }

调用高德地图SDK,显示地图组件:

ts 复制代码
import { AMap, MapsInitializer, MapView, MapViewComponent, MapViewManager, } from '@amap/amap_lbs_map3d';
// 配置 API KEY
MapsInitializer.setApiKey("xxx");
MapViewManager.getInstance().registerMapViewCreatedCallback((mapview?: MapView, mapViewName?: string) => {
  if (!mapview) {
    return;
  }
  let mapView = mapview;
  mapView.onCreate();
  mapView.getMapAsync((map) => {
    let aMap: AMap = map;
  })
})

@Entry
@Component
struct Index {
  build() {
    Row() {
      MapViewComponent()
        .width('100%')
        .height('100%')
    }
  }
}

调用

dart 复制代码
PlartformCall.redirectNative('pages/Amap');

注意事项

如果在运行时,遇到以下错误, 根据官方提醒, 需要配置 useNormalizedOHMUrl

复制代码
 ERROR: Bytecode HARs: [@amap/amap_lbs_map3d, @amap/amap_lbs_common] not supported when useNormalizedOHMUrl is not true.

打开文件 /ohos/build-profile.json5, 添加以下配置

json 复制代码
 		{
 		  "app": {
 		    "products": [
 		      {
 		         "buildOption": {
 		           "strictMode": {
		             "useNormalizedOHMUrl": true
 		           }
 		         }
 		      }
 		    ]
 		  }
 		}

截图

源码

https://gitee.com/zacks/flutter-ohos-demo

参考资料

相关推荐
芙莉莲教你写代码1 小时前
Flutter 框架跨平台鸿蒙开发 - 魔术教学
flutter·华为·harmonyos
纯爱掌门人1 小时前
鸿蒙文件预览开发实践:从打开文件到加速感知
华为·harmonyos
weixin_443478512 小时前
Flutter第三方常用组件包之路由管理
前端·javascript·flutter
云和数据.ChenGuang3 小时前
当智能体遇上原生鸿蒙:开启下一代操作系统的“智慧觉醒”
华为·harmonyos
左手厨刀右手茼蒿3 小时前
Flutter 三方库 bs58 的鸿蒙化适配指南 - 在鸿蒙系统上构建极致、高效的 Base58 数字货币与区块链数据编解码引擎
flutter·harmonyos·鸿蒙·openharmony
加农炮手Jinx3 小时前
Flutter 组件 substrate_bip39 的适配 鸿蒙Harmony 实战 - 驾驭区块链级助记词原语、实现鸿蒙端金融级 BIP39 安全私钥推导方案
flutter·harmonyos·鸿蒙·openharmony·substrate_bip39
左手厨刀右手茼蒿3 小时前
Flutter 组件 substrate_bip39 的适配 鸿蒙Harmony 实战 - 驾驭区块链级 BIP39 安全底座、实现鸿蒙端私钥派生与国密级密钥保护方案
flutter·harmonyos·鸿蒙·openharmony·substrate_bip39
加农炮手Jinx3 小时前
Flutter 三方库 fast_base58 的鸿蒙化进阶指南 - 挑战编解码吞吐量极限、助力鸿蒙端大规模区块链与分布式存储数据处理
flutter·harmonyos·鸿蒙·openharmony·fast_base58
里欧跑得慢3 小时前
Flutter 三方库 ethereum 鸿蒙分布式区块链数字资产上链钱包适配突破:接通 JSON-RPC 加密管线深入打通智能合约闭环实现高价值数字加密交互-适配鸿蒙 HarmonyOS ohos
分布式·flutter·harmonyos
2501_920627613 小时前
Flutter 框架跨平台鸿蒙开发 - 压力管理助手应用
flutter·华为·harmonyos