Skip to main content
The MapManager class exposes a stream property that you can listen for any map-related events. A map event is an instance of MapManagerMessage, with various constructors for each specific event.

Supported Events

Example of all currently supported events:
import 'dart:async';
import 'package:tomtom_flutter_map/tomtom_flutter_map.dart';

final StreamSubscription<MapManagerMessage> subscription =
    _mapManager.stream.listen((event) {
  switch (event) {
    case MapManagerMessageMapIsReady _:
      // map is rendered and ready to be interacted with
      // might not get called if map is already ready when listened
      print('Map is ready');

    case MapManagerMessageOnStyleLoad _:
      // map style has been loaded
      print('Map style loaded.');

    case MapManagerMessageCameraTrackingModeChanged(:final mode):
      // camera tracking mode has changed (ex followNorthUp, none)
      print('Camera tracking mode changed to $mode');

    case MapManagerMessageOnRouteSelected(:final mapRouteId):
      // user touched on a visible route in the map
      print('Route $mapRouteId selected.');

    case MapManagerMessageOnAnnotationSelected(:final annotationId):
      // user touched on an annotation in the map (such as a marker)
      print('Annotation $annotationId selected.');

    case MapManagerMessageOnCameraChanged(:final position):
      // camera positioning has changed
      print(
        'Camera positioning changed to ${position.latitude},${position.longitude}',
      );

    case MapManagerMessageOnLongPress(:final position):
      // user has long pressed in the map
      print('Long pressed on ${position.latitude},${position.longitude}');

    default:
      print('Unhandled event: ${event.runtimeType}');
  }
});
Be wary of race conditions when listening for MapManagerMessageMapIsReady events. See Map Readiness for more information.
Don’t forget to cancel the subscription when you’re done listening, usually in your Widget’s dispose() method:
subscription.cancel();
Alternatively, you can listen for a specific event:
final subscription = _mapManager.stream
    .where((event) => event is MapManagerMessageOnLongPress)
    .cast<MapManagerMessageOnLongPress>()
    .listen((longPressEvent) {
        // user long pressed in the map
    });

Map Readiness

The MapManagerMessageMapIsReady constructor notifies the map is ready to be interacted with. This is an important event, because calling most methods in the MapManager class will cause an exception if the map is not ready. We’ve built a helper async method you can use to easily verify or wait for the map to be ready:
// wait for map to be ready
await _mapManager.waitUntilReady();

// now it's safe to call map manager methods
await mapManager.setMapStyleMode(MapManagerStyleMode.dark);
This method is safe to be called as many times as needed, at any point in time.

Style Loaded

After the map is initially loaded, the default style provided in the TomTomMap’s widget options will be loaded and a MapManagerMessageOnStyleLoad event will be emitted. Additional style loaded events will be emitted when changing the map style mode or container. In a similar manner to map readiness events, it is important to wait for the style to be loaded before calling methods applicable to the style. For example, calling mapManager.showLayer('layerName') while the style is loading will fail.