HarmonyOS Flutter Practice: 20. Flutter integrates Amap and renders on the same layer
This article takes same-layer rendering as an example to introduce how to integrate Amap
This article takes same-layer rendering as an example to introduce how to integrate Amap
For the complete code, see Flutter HarmonyOS Edition Demo [1]
Overview
Dart side
The core code is as follows, using OhosView to carry the native view
OhosView(
viewType: 'com.shaohushuo.app/customView',
onPlatformViewCreated: _onPlatformViewCreated,
creationParams: const <String, dynamic>{'initParams': 'hello world'},
creationParamsCodec: const StandardMessageCodec(),
)
Where viewType is the name of the custom ohosView, onPlatformViewCreated is the creation completion callback, creationParams is the parameters passed in during creation, and creationParamsCodec is the parameter encoding format.
ArkTS side
Here we follow the example in “How to use PlatformView”. First, we need to create a view that displays Amap. The core code is as follows:
Complete file AmapWidgetFactory.ets [2]
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;
})
})
@Component
struct ButtonComponent {
@Prop params: Params
customView: AmapWidgetView = this.params.platformView as AmapWidgetView build() {
Row() {
MapViewComponent().width('100%').height('100%')
}
}
}
Next, create an AmapWidgetFactory.ets [3]
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);
}
}
Finally, you need to create an AmapWidgetPlugin.ets [4]
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 {}
}
After the plugin is created, remember to register it in EntryAbility
this.addPlugin(new AmapWidgetPlugin())
It should be noted that the view ID must be consistent on both sides, such as ‘com.shaohushuo.app/customView’ here, otherwise it will not display properly
screenshot
References
How to use PlatformView [5]
New PlatformView same-layer rendering solution [6]
[1] Flutter HarmonyOS version Demo: https://gitee.com/zacks/flutter-ohos-demo/commit/2b16c6f34abd4c61eea89805bc314a10874c305f
[2] AmapWidgetFactory.ets: https://gitee.com/zacks/flutter-ohos-demo/blob/master/packages/apps/ohos_app/ohos/entry/src/main/ets/entryability/AmapWidget/AmapWidgetView.ets
[3] AmapWidgetFactory.ets: https://gitee.com/zacks/flutter-ohos-demo/blob/master/packages/apps/ohos_app/ohos/entry/src/main/ets/entryability/AmapWidget/AmapWidgetFactory.ets
[4] AmapWidgetPlugin.ets: https://gitee.com/zacks/flutter-ohos-demo/blob/master/packages/apps/ohos_app/ohos/entry/src/main/ets/entryability/AmapWidget/AmapWidgetPlugin.ets
[5] How to use PlatformView: https://gitcode.com/openharmony-sig/flutter_samples/blob/br_3.7.12-ohos-1.0.5/ohos/docs/04_development/%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8PlatformView.md
[6] PlatformView same-layer rendering new solution: https://gitcode.com/openharmony-sig/flutter_samples/blob/master/ohos/docs/04_development/PlatformView%E5%90%8C%E5%B1%82%E6%B8%B2%E6%9F%93%E6%96%B9%E6%A1%88%E9%80%82%E9%85%8D%E5%88%87%E6%8D%A2%E6%8C%87%E5%AF%BC.md#platformview%E6%96%B0%E6%96%B9%E6%A1%88
Source: HarmonyOS Flutter Practice: 20. Flutter integrates Amap and renders on the same layer | LinkedIn