From 0cf4bf3cda675e34655454a8db3e2658f23405c8 Mon Sep 17 00:00:00 2001 From: Joscha <34318751+josxha@users.noreply.github.com> Date: Tue, 5 Nov 2024 14:00:28 +0100 Subject: [PATCH] use event stream, add note to readme --- flutter_map_maplibre/README.md | 5 ++++ .../lib/src/maplibre_layer.dart | 25 ++++++++++++++----- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/flutter_map_maplibre/README.md b/flutter_map_maplibre/README.md index 855790c..37352fa 100644 --- a/flutter_map_maplibre/README.md +++ b/flutter_map_maplibre/README.md @@ -50,6 +50,11 @@ the [hosted example app](https://flutter-map-plugins.web.app/). Here we add a `MapLibreMap` as a layer to `FlutterMap` and let flutter_map handle all gesture inputs. +> [!WARNING] +> NOTE: The `MapLibreLayer` has currently a known bug causing it to have a high +> delay and throwing exceptions to the +> console: https://github.com/josxha/flutter_map_plugins/issues/54 + ```dart @override Widget build(BuildContext context) { diff --git a/flutter_map_maplibre/lib/src/maplibre_layer.dart b/flutter_map_maplibre/lib/src/maplibre_layer.dart index 1f38619..5de1c18 100644 --- a/flutter_map_maplibre/lib/src/maplibre_layer.dart +++ b/flutter_map_maplibre/lib/src/maplibre_layer.dart @@ -1,3 +1,5 @@ +import 'dart:async'; + import 'package:flutter/widgets.dart'; import 'package:flutter_map/flutter_map.dart' as fm; import 'package:flutter_map_maplibre/src/extensions.dart'; @@ -31,19 +33,24 @@ class MapLibreLayer extends StatefulWidget { class _MapLibreLayerState extends State { MapController? _controller; + StreamSubscription? _streamSub; @override Widget build(BuildContext context) { final fmCamera = fm.MapCamera.of(context); - // final fmController = fm.MapController.of(context); + final fmController = fm.MapController.of(context); // final fmOptions = fm.MapOptions.of(context); // sync the FlutterMap movement with MapLibreMap - _controller?.moveCamera( - center: fmCamera.center.toPosition(), - zoom: fmCamera.zoom - 1, - bearing: -fmCamera.rotation, - ); + _streamSub ??= fmController.mapEventStream.listen((event) { + if (event case fm.MapEventWithMove()) { + _controller?.moveCamera( + center: event.camera.center.toPosition(), + zoom: event.camera.zoom - 1, + bearing: -event.camera.rotation, + ); + } + }); return MapLibreMap( options: MapOptions( @@ -63,4 +70,10 @@ class _MapLibreLayerState extends State { children: widget.children, ); } + + @override + void dispose() { + _streamSub?.cancel(); + super.dispose(); + } }