Skip to content

Commit

Permalink
use event stream, add note to readme
Browse files Browse the repository at this point in the history
  • Loading branch information
josxha committed Nov 5, 2024
1 parent 46aed69 commit 0cf4bf3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
5 changes: 5 additions & 0 deletions flutter_map_maplibre/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
25 changes: 19 additions & 6 deletions flutter_map_maplibre/lib/src/maplibre_layer.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -31,19 +33,24 @@ class MapLibreLayer extends StatefulWidget {

class _MapLibreLayerState extends State<MapLibreLayer> {
MapController? _controller;
StreamSubscription<fm.MapEvent>? _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(
Expand All @@ -63,4 +70,10 @@ class _MapLibreLayerState extends State<MapLibreLayer> {
children: widget.children,
);
}

@override
void dispose() {
_streamSub?.cancel();
super.dispose();
}
}

0 comments on commit 0cf4bf3

Please sign in to comment.