Controllers & Cameras

flutter_map makes use of InheritedModel to share 3 'aspects' with its built children:

  • MapController: use to programatically control the map camera & access some helper functionality - control camera

  • MapCamera: use to read the current state/position of the map camera & access some helper functionality that depends on the camera (such as latlngToPoint) - read camera

MapOptions is also an aspect, which reflects the MapOptions defined on the FlutterMap.options parameter.

However, it is mostly irrelevant, except for when Layers.

Accessing Aspects Within Descendants

All 3 aspects can be retrieved from within the context of a FlutterMap, which all built descendants should have access to. This usually means from within a layer: anywhere where there is at least one 'visible' builder method between the FlutterMap and the invocation.

Use the static of (or null-safe maybeOf) method to access the inherited aspect. For example, to access the MapCamera:

final inheritedCamera = MapCamera.of(context);

This will attach the widget to the state of the map, causing it to rebuild whenever the depended-on aspects change. See 2. Hooking Into Inherited State for more information.

Accessing Aspects Elsewhere

MapCamera

To access the MapCamera outside of a FlutterMap descendant, first setup an external MapController, as guided below.

Then use the camera getter on the MapController instance.

MapController

For more information about correctly setting up an external(ly accessible) MapController, see:

External Custom Controllers

MapOptions

It is not possible to access the MapOptions in this way outside of FlutterMap descendants.

This is because it is not changed by FlutterMap, and so that would be unnecessary.

Was this helpful?