鸿蒙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

参考资料

相关推荐
钉先生贾17 小时前
HarmonyOS 7.0 多设备协同实战 35:用户撤销分享链接后,怎样使缓存入口同时失效
缓存·华为·harmonyos
恋猫de小郭1 天前
Flutter Perfetto:Dart Heap Sampling 终于要接入 Android 原生内存分析了
android·前端·flutter
羽落•无痕(苏晗默)1 天前
华为数通IA——16、数据中心网络基础:从通用计算到智能计算的网络演进
网络·计算机网络·网络安全·华为·信息与通信
陈橘又青1 天前
把加密 DNS 关进本地 5353:dnscrypt-proxy 的鸿蒙 PC 复刻笔记
华为·harmonyos
kirk_wang1 天前
HarmonyOS 7.0 A2A多轮实战:缺少健康摘要授权时如何安全暂停
华为·harmonyos
程序员老刘1 天前
Flutter版本选择指南:尽早升级3.47.9 | 2026年11月
flutter·ai编程·客户端
JosieBook2 天前
【鸿蒙开源】把 Windows 任务栏上的流量表搬到鸿蒙 PC:TrafficMonitor 1.86 的 C++ 内核移植实践
c++·windows·harmonyos
网易云信2 天前
聊天图片能“按意思搜”了:云信 NIM 鸿蒙 SDK 的两项升级
harmonyos
HarmonyOS_SDK2 天前
【HarmonyOS API 26】端云通信加速,助力京东打造稳定流畅的购物直播体验
harmonyos
kirk_wang2 天前
HarmonyOS 7.0 A2A任务链实战:用getRelatedTasks核对随访材料来源
华为·harmonyos