flutter_map Docs
Project Links💝 Support Us
v6
v6
  • flutter_map
  • 🏗️Showcase
  • 💝Supporters
  • ✏️Credits & Contributing
  • Getting Started
    • How Does It Work?
      • Raster vs Vector Tiles
    • Demonstration
    • Installation
    • Examples
    • Migrating To v6
  • Usage
    • Base Widget
    • Options
      • Interaction Options
    • Layers
    • Programmatic Control
      • Control Camera
      • Get Camera
      • 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
  • Tile Servers
    • Using Mapbox
    • Using Thunderforest
    • Using Stadia Maps
    • 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
  • Usage Inside Of A FlutterMap Child
  • Usage Outside Of FlutterMap
  • Initialisation
  • Usage Inside initState()
  • Animated Movements

Was this helpful?

Export as PDF
  1. Usage
  2. Programmatic Control

Control Camera

PreviousProgrammatic ControlNextGet Camera

Last updated 1 year ago

Was this helpful?

To control the map (such as moving it to a new position and zoom level), you'll need a MapController. The controller does not provide access to the current viewport/camera: that is the responsibility of .

Usage Inside Of A FlutterMap Child

To control the map from within the context of a FlutterMap widget, use MapController.of(context).

If this throws a StateError, try wrapping the concerned widget in a Builder, to ensure the FlutterMap widget is parenting the BuildContext. If this has no effect, use Usage Outside Of FlutterMap instead.

Usage Outside Of FlutterMap

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,
        // ...
    );

Usage Inside initState()

Sometimes, it is necessary MapController in initState() before the map has been built, for example to attach an event listener (Listen To Events). This is not directly possible, as the map must be built for the controller to be attached.

Instead, use the MapOptions.onMapReady callback. The initialised MapController 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
            },
        ),
    );

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

Animated Movements

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

Calling this method in a build method will cause the widget to automatically rebuild if the MapController changes. See for more information.

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!

issue #1507
community maintained plugin flutter_map_animations
MapCamera
MapController class - flutter_map library - Dart API
Logo
#2.-hooking-into-inherited-state