当前位置: 首页 > news >正文

鸿蒙Flutter实战:20. Flutter集成高德地图,同层渲染

本文以同层渲染为例,介绍如何集成高德地图

完整代码见 Flutter 鸿蒙版 Demo

概述

Dart 侧

核心代码如下,通过 OhosView 来承载原生视图

OhosView(
      viewType: 'com.shaohushuo.app/customView',
      onPlatformViewCreated: _onPlatformViewCreated,
      creationParams: const <String, dynamic>{'initParams': 'hello world'},
      creationParamsCodec: const StandardMessageCodec(),
    )

其中 viewType 为自定义的 ohosView 的名称,onPlatformViewCreated 为创建完成回调,creationParams 为创建时传入的参数,creationParamsCodec 为参数编码格式。

ArkTS 侧

这里面我们按照《如何使用PlatformView》中的示例操作,首先需要创建一个显示高德地图的视图,其核心代码如下:

完整文件 AmapWidgetFactory.ets


MapsInitializer.setApiKey("e4147e927a1f63a0acff45cecf9419b5");
MapViewManager.getInstance().registerMapViewCreatedCallback((mapview?: MapView, mapViewName?: string) => {
  if (!mapview) {
    return;
  }
  let mapView = mapview;
  mapView.onCreate();
  mapView.getMapAsync((map) => {
    let aMap: AMap = map;
  })
})


struct ButtonComponent {
   params: Params
  customView: AmapWidgetView = this.params.platformView as AmapWidgetView

  build() {
    Row() {
      MapViewComponent().width('100%').height('100%')
    }
  }
}

接下来创建一个 AmapWidgetFactory.ets

export class AmapWidgetFactory extends PlatformViewFactory {
  message: BinaryMessenger;

  constructor(message: BinaryMessenger, createArgsCodes: MessageCodec<Object>) {
    super(createArgsCodes);
    this.message = message;
  }

  public create(context: common.Context, viewId: number, args: Object): PlatformView {
    return new AmapWidgetView(context, viewId, args, this.message);
  }
}

最终需要创建一个 AmapWidgetPlugin.ets

export class AmapWidgetPlugin implements FlutterPlugin {
  getUniqueClassName(): string {
    return 'AmapWidgetPlugin';
  }

  onAttachedToEngine(binding: FlutterPluginBinding): void {
    binding.getPlatformViewRegistry()?.
    registerViewFactory('com.shaohushuo.app/customView', new AmapWidgetFactory(binding.getBinaryMessenger(), StandardMessageCodec.INSTANCE));
  }

  onDetachedFromEngine(binding: FlutterPluginBinding): void {}
}

插件创建好之后,记得在 EntryAbility 中注册插件

 this.addPlugin(new AmapWidgetPlugin())

需要注意的是,视图ID一定要两侧保持一致,如这里名为 ‘com.shaohushuo.app/customView’,否则无法正常显示

截图

参考资料

  • 如何使用PlatformView
  • PlatformView同层渲染新方案

相关文章:

  • 老是忘记package.json,备忘一下 webpack 环境下 Vue Cli 和 Vite 命令行工具对比
  • SpringMVC请求和响应
  • 只出现一次的数字(js实现,LeetCode136)
  • Vue 中使用 ECharts
  • IntelliJIDEA中实现Spring Boot多实例运行:修改配置与批量启动详解
  • How to install vmware workstation pro on Linux mint 22
  • 在 spring tool suite 如何安装 lombok
  • 有关pip与conda的介绍
  • VMware Fusion 虚拟机Mac版 安装CentOS 7
  • TypeScript 静态类型检查:提升前端开发的可靠性与效率
  • DeepSeek结合MCP Server与Cursor,实现服务器资源的自动化管理
  • CNN和LSTM的计算复杂度分析
  • 5.2 WPF数据绑定简单示例1
  • 力扣DAY29 | 热100 | 删除链表的倒数第N个结点
  • tauri2.0安卓端开发踩坑之旅
  • 云安全之k8s未授权漏洞总结
  • 使用 OCRmyPDF 将扫描 PDF 转为可搜索文档和文本文件
  • 可发1区的超级创新思路(python\matlab实现):基于周期注意力机制的TCN-Informer时间序列预测模型
  • 深入解析 PKI(公钥基础设施):原理、应用与安全保障
  • 【科研绘图系列】R语言绘制重点物种进化树图(taxa phylogenetic tree)
  • 石家庄个人建网站/鼓楼网站seo搜索引擎优化
  • wordpress 更新主题/优化大师的三大功能
  • 迎访问备案网站管理系统/seo职位具体做什么
  • 了解网站基本知识/百度提交收录入口
  • 网络科技有限公司网站建设策划书/查询网站服务器
  • 企业网站建设框架图/seo品牌