flutter_map Docs
Project Links💝 Support Us
v4
v4
  • flutter_map
  • How Does It Work?
    • Raster vs Vector Tiles
  • Getting Started
    • Installation
    • Additional Setup
    • Examples
    • Migrating To v4
  • Usage
    • Base Widget
    • Options
    • Layers
    • Controller
    • Full API Reference
  • Layers
    • Tile Layer
      • Tile Providers
    • Marker Layer
    • Polygon Layer
    • Polyline Layer
    • Circle Layer
    • Overlay Image Layer
    • Attribution Layer
    • WMS Usage
  • Plugins
    • Plugins List
    • Making A Plugin
      • Creating New Tile Providers
      • Creating New Layers
  • Tile Servers
    • Using Mapbox
    • Using Thunderforest
    • Using Stadia Maps
    • Offline Mapping
    • Other Options
  • Frequently Asked Questions
  • Contributing
  • Credits
Powered by GitBook

© flutter_map Authors & Maintainers

On this page
  • Initialisation
  • Usage In initState()
  • Animation

Was this helpful?

Export as PDF
  1. Usage

Controller

PreviousLayersNextTile Layer

Last updated 2 years ago

Was this helpful?

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) individually

  • Re-initialise a new MapController, and keep it in an external state system

Usage In 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.

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
            },
        ),
    );

That method will, however, not work if the map is not built on the first frame. This may be the case if it is, for example, in a FutureBuilder.

Using them as a reaction to a map event is still fine.

Animation

The example application also includes a page demonstrating a custom animated map movement.

If you still get issues, and FlutterMap is located inside a PageView, ListView or another complex lazy layout, try setting keepAlive true in MapOptions: .

This isn't a problem however! The MapOptions contains an onMapReady callback (see ) is called when the map is initialised, and the initialised map controller can be used freely within it.

It may also be possible to use SchedulerBinding or WidgetsBinding in initState to run a method after the first frame has been built, as detailed here: . You'll probably see this approach in many older projects.

MapController methods that change the position of the map should not be used instantly in onMapReady - see .

Whilst animated movements through MapControllers aren't built-in, the provides this, and much more!

https://stackoverflow.com/a/64186549/11846040
issue #1507
community maintained plugin flutter_map_animations
Permanent Rules
Event Handling
LogoMapController class - flutter_map library - Dart API