flutter_map Docs
Project Links💝 Support Us
v7
v7
  • flutter_map
  • 🏗️Why Choose flutter_map?
  • 💝Supporters
  • ✏️Credits & Contributing
  • Getting Started
    • How Does It Work?
      • Raster vs Vector Tiles
    • Installation
    • Examples
    • v7 Information
  • Usage
    • Base Widget
    • Options
      • Interaction Options
      • Custom CRSs
    • Layers
    • Programmatic Interaction
      • Controllers & Cameras
      • External Custom Controllers
      • Listen To Events
    • Full API Reference
  • Layers
    • Tile Layer
      • Tile Providers
      • WMS Usage
    • Marker Layer
    • Polygon Layer
    • Polyline Layer
    • Circle Layer
    • Overlay Image Layer
    • Attribution Layer
    • Layer Interactivity
      • Hit Testing Behaviour
  • Tile Servers
    • Using Google Maps
    • Using Mapbox
    • Using Thunderforest
    • Using Tracestrack
    • Using Stadia Maps
    • Using Lima Labs
    • Using Bing Maps
    • Offline Mapping
    • Other Options
  • Plugins
    • Plugins List
    • Creating A Plugin
      • Creating New Tile Providers
      • Creating New Layers
Powered by GitBook

© flutter_map Authors & Maintainers

On this page
  • Accessing Aspects Within Descendants
  • Accessing Aspects Elsewhere
  • MapCamera
  • MapController
  • MapOptions

Was this helpful?

Export as PDF
  1. Usage
  2. Programmatic Interaction

Controllers & Cameras

PreviousProgrammatic InteractionNextExternal Custom Controllers

Last updated 1 year ago

Was this helpful?

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 Creating New 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 for more information.

Using this method directly in the children list (not inside another widget), and in any MapOptions callback, is not possible: there is no* builder method between the FlutterMap and the children or callback.

Instead, follow Accessing Aspects Elsewhere, or, wrap the necessary layers with a Builder widget. For example, the code snippet below hides a TileLayer when above zoom level 13:

children: [
    Builder(
        builder: (context) {
            if (MapCamera.of(context).zoom < 13) return SizedBox.shrink();
            return TileLayer();
        },
    ),
],

Accessing Aspects Elsewhere

MapCamera

Then use the camera getter on the MapController instance.

Avoid using this method to access the camera when MapCamera.of() is available.

MapController

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

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.

To access the MapCamera outside of a FlutterMap descendant, first .

External Custom Controllers
setup an external MapController, as guided below
2. Hooking Into Inherited State