Controller
To programatically interact with the map (such as panning, zooming and rotating) and receive it's events, you'll need a MapController
.
If building a custom layer (Creating New Layers), consider using FlutterMapState
directly instead.
Initialisation
To use a MapController
, it must initialised like any other object and then passed to the FlutterMap
. This attaches them until the map is disposed.
final mapController = MapController();
@override
Widget build(BuildContext context) =>
FlutterMap(
mapController: mapController,
...
);
Avoid disconnecting the map from the controller, as it can cause problems. If you need to change the map's contents:
Change its
children
(layers) individuallyRe-initialise a new
MapController
, and keep it in an external state system
If you still get issues, and FlutterMap
is located inside a PageView
, ListView
or another complex lazy layout, try setting keepAlive
true
in MapOptions
: Permanent Rules.
Usage In initState()
initState()
It is a fairly common requirement to need to use the MapController
in initState()
, before the map has been built. Unfortunately, this is not possible, as the map must be built for the controller to be attached.
This isn't a problem however! The MapOptions
contains an onMapReady
callback (see Event Handling) is called when the map is initialised, and the initialised map controller can be used freely within it.
final mapController = MapController();
@override
Widget build(BuildContext context) =>
FlutterMap(
mapController: mapController,
options: MapOptions(
onMapReady: () {
mapController.mapEventStream.listen((evt) {});
// And any other `MapController` dependent non-movement methods
},
),
);
MapController
methods that change the position of the map should not be used instantly in onMapReady
- see issue #1507.
Using them as a reaction to a map event is still fine.
Animation
Whilst animated movements through MapController
s aren't built-in, the community maintained plugin flutter_map_animations
provides this, and much more!
The example application also includes a page demonstrating a custom animated map movement.
Last updated
Was this helpful?