githubEdit

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.

circle-exclamation

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 center of the map

  • the current rotation of the map (in degrees, where 0Β° is North)

  • the current zoom level 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 level

    • fitCamera, which allows more advanced positioning using coordinate boundaries and screen-space padding

  • rotate the 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.

circle-info

MapOptions is also an aspect of the same inherited model, but this is not useful here since it cannot be exposed outside of map layers.

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:

circle-exclamation

Reacting to map events

To imperatively react to changes to the map camera, there's multiple methods available.

circle-info

Remember that when using MapCamera.of, that widget will automatically rebuild when the camera changes.

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:

  • onTap

  • onLongPress

  • onPointerDown/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 moves

  • onMapReady: called when the MapController is attached

For example:

circle-info

The onTap callback and MapEventTap event may be emitted 250ms after the actual tap occurred, as this is the acceptable delay between the two taps in a double tap zoom gesture.

If your project would benefit from a faster reaction to taps, disable the double tap zoom gesture, which will allow taps to be handled immediately:

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:

Last updated

Was this helpful?