Get started

Controls and panels

MapControls floats the map's controls in one of its corners. Each control drives the map it is handed, and each panel opens beside its toggle.

Add the control stack

import 'package:flutter/widgets.dart';
import 'package:mountaya_client/mountaya_client.dart';
import 'package:mountaya_elements_flutter/mountaya_elements_flutter.dart';

class TrailMap extends StatefulWidget {
  const TrailMap({super.key, required this.mountaya});

  final Mountaya mountaya;

  @override
  State<TrailMap> createState() => _TrailMapState();
}

class _TrailMapState extends State<TrailMap> {
  late final MapEngine map = MapEngine(widget.mountaya);

  @override
  void dispose() {
    map.close();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) => Stack(
    children: [
      MapView(engine: map),
      // Zoom, compass, layers and preferences show unless you turn them off.
      MapControls(map: map, geolocate: true, pois: true),
    ],
  );
}

A panel opens in the nearest Overlay. A Navigator provides one; under a bare WidgetsApp, wrap your screen in an Overlay as Trail does.

Pick the control for the job

Compose a control on its own to answer a press your own way:

import 'package:flutter/widgets.dart';
import 'package:mountaya_client/mountaya_client.dart';
import 'package:mountaya_elements_flutter/mountaya_elements_flutter.dart';

class TrailMap extends StatefulWidget {
  const TrailMap({super.key, required this.mountaya});

  final Mountaya mountaya;

  @override
  State<TrailMap> createState() => _TrailMapState();
}

class _TrailMapState extends State<TrailMap> {
  late final MapEngine map = MapEngine(widget.mountaya);
  bool _exporting = false;

  @override
  void dispose() {
    map.close();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) => Stack(
    children: [
      MapView(engine: map),
      Positioned(
        top: 16,
        right: 16,
        child: Column(
          children: [
            ZoomControl(map: map),
            CompassControl(map: map),
            GeolocateControl(map: map),
            DownloadControl(
              enabled: !_exporting,
              onTap: () => setState(() => _exporting = true),
            ),
            const PanelControl(
              panel: MapPanel('filters'),
              label: 'Filters',
              glyph: 'preferences',
              help: 'Huts only',
              child: Text('Huts only'),
            ),
          ],
        ),
      ),
    ],
  );
}

Choose terrains, points of interest and units

The panels the stack opens are widgets too, so you can place one where you like:

import 'package:flutter/widgets.dart';
import 'package:mountaya_client/mountaya_client.dart';
import 'package:mountaya_client/rest_schema.dart' as rest_schema;
import 'package:mountaya_elements_flutter/mountaya_elements_flutter.dart';
import 'package:mountaya_geo/mountaya_geo.dart';

class TrailMap extends StatefulWidget {
  const TrailMap({super.key, required this.mountaya});

  final Mountaya mountaya;

  @override
  State<TrailMap> createState() => _TrailMapState();
}

class _TrailMapState extends State<TrailMap> {
  late final MapEngine map = MapEngine(widget.mountaya);
  // The selection drives the map; the State that makes it closes it.
  late final LayersSelectionEngine selection = LayersSelectionEngine(map: map);
  UnitPreferences _units = const UnitPreferences(
    language: 'fr',
    length: rest_schema.UnitLength.metric,
  );

  @override
  void dispose() {
    selection.close();
    map.close();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) => Column(
    children: [
      LayersPanel(
        selection: selection,
        thumbnail: (choice, slug) => AssetImage('assets/layers/$slug.webp'),
      ),
      PoiPanel(
        map: map,
        groups: [
          PoiPanelGroup(id: 'water', label: 'Water', keys: const ['drinking-water']),
        ],
      ),
      PreferencesPanel(
        preferences: _units,
        onPreferencesChanged: (next) => setState(() => _units = next),
        map: map,
      ),
    ],
  );
}

Move through time

A forecast overlay, such as snow-depth, is drawn for the instant you pin. Offer the time slider in the layers panel:

import 'dart:async';

import 'package:flutter/widgets.dart';
import 'package:mountaya_client/mountaya_client.dart';
import 'package:mountaya_elements_flutter/mountaya_elements_flutter.dart';

class TrailMap extends StatefulWidget {
  const TrailMap({super.key, required this.mountaya});

  final Mountaya mountaya;

  @override
  State<TrailMap> createState() => _TrailMapState();
}

class _TrailMapState extends State<TrailMap> {
  late final MapEngine map = MapEngine(widget.mountaya);

  @override
  void initState() {
    super.initState();
    unawaited(map.setOverlay('snow-depth'));
  }

  @override
  void dispose() {
    map.close();
    super.dispose();
  }

  // The layers panel offers the time slider over the snow-depth forecast.
  @override
  Widget build(BuildContext context) => Stack(
    children: [
      MapView(engine: map),
      MapControls(map: map, offersTime: true),
    ],
  );
}

Offer the time only where your organization's overlay time control allows it.

Next steps

Last updated