Programmatic Interaction
In addition to the user interacting with the map, for example by using their cursor, trackpad, or touchscreen, the map can be controlled programmatically.
Changing the state of MapOptions.initial* will not update the map. Map control in 'flutter_map' is imperative.
Programmatic interaction consists of two parts: reading and setting information about the map.
To use programmatic interaction, it's important to understand the difference between the map's 'camera' and the map's 'controller', and their relationship.
Camera
The map's camera - or MapCamera - is an object that holds information to be read about the map's current viewport, such as:
the coordinates of the geographic location at the
centerof the mapthe current
rotationof the map (in degrees, where 0Β° is North)the current
zoomlevel of the map
For example:
Controller
Similarly to other map client projects, the controller - MapController - allows the map's viewport/camera to be modified/set using methods, such as:
move the camera to a new location and zoom level, using either:
move, which accepts a new center coordinate and zoom levelfitCamera, which allows more advanced positioning using coordinate boundaries and screen-space padding
rotatethe camera to a new number of degrees from North
For example:
Accessing the 'aspects'
Together, the MapCamera and MapController are two aspects of the MapInheritedModel.
This means that you can get both the camera and the controller from within a layer of the map, given the map's BuildContext.
The MapCamera.of & MapController.of methods accept this context, and return their respective aspect. They also subscribe the layer for further updates: using MapCamera.of means that widget/layer will rebuild every time the MapCamera changes (for example, when the map's location changes).
For example:
Using MapController.of(context).camera is an anti-pattern and not recommended.
If you want to access either aspect from outside of the map - for example, to reposition the map when the user presses a button, or to use the map's location in an API call - you'll need to:
Attach the controller to the map
The default controller that FlutterMap uses and passes around internally can be overridden with the external controller using FlutterMap.controller.
For example:
The MapController does not become safe to use until after it has been fully initialised and attached to the map widget, which occurs during the first build of the map.
This means it cannot be used directly within initState, for example.
See The controller is not safe to use until attached for more information.
Then, the external controller controls the map, and you can access the MapCamera from outside of the map using MapController.camera. For example:
Pitfalls of an external controller
Reacting to map events
To imperatively react to changes to the map camera, there's multiple methods available.
Simple events
If you prefer a callback-based pattern and need to capture user interaction events with the widget (with limited information about the event or its effect on the map itself), the following callbacks are available through MapOptions:
onTaponLongPressonPointerDown/onPointerMove/onPointerUp/onPointerHover/onPointerCancel
These callbacks are also available, which are not strictly caused by user interaction events and give information about the map, but are provided for ease-of-use:
onPositionChanged: called when the map movesonMapReady: called when theMapControlleris attached
For example:
Multiple or complex event handling
There's two methods to handle raw MapEvents, which are emitted whenever the MapCamera changes, and contain detailed information, such as the source of the event, and, for some events, the old and new camera.
MapOptions has a callback named onMapEvent. For example:
In addition to controlling the map, the MapController also has a getter called mapEventStream. For example:
Last updated
Was this helpful?