Get started

Quickstart

A Flutter app for Android and iOS that shows a topographic map and draws the Tour du Mont-Blanc on it.

Before you start

You need Flutter 3.44 or newer, an Android emulator or an iOS 15.5 simulator, and a publishable key with the tiles and publishing scopes from Organization → API keys in the Mountaya app.

Install

flutter create trail && cd trail
flutter pub add mountaya_client mountaya_geo mountaya_elements_flutter

Then raise the app's floors to iOS 15.5 and Android API level 24, as Install and set up shows.

Add your key

Create .env at the project's root, and add it to .gitignore:

MOUNTAYA_PUBLISHABLE_KEY=pk_your_publishable_key

Show the map

Replace lib/main.dart with this:

import 'dart:async';

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

// Compiled in from --dart-define-from-file=.env.
const publishableKey = String.fromEnvironment('MOUNTAYA_PUBLISHABLE_KEY');

void main() => runApp(
  WidgetsApp(
    color: const Color(0xff1c7ed6),
    builder: (context, _) => const TrailMap(),
  ),
);

class TrailMap extends StatefulWidget {
  const TrailMap({super.key});

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

class _TrailMapState extends State<TrailMap> {
  final mountaya = Mountaya(credential: .publishable(key: publishableKey));
  late final map = MapEngine(mountaya);

  @override
  void initState() {
    super.initState();
    unawaited(map.setTerrain());
  }

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

  @override
  Widget build(BuildContext context) => MapView(engine: map);
}

MapEngine holds what the map shows, and MapView draws it. Your State makes both and closes them.

Draw an itinerary

Replace TrailMap with this version. The camera frames the itinerary once it is drawn:

import 'dart:async';

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

const publishableKey = String.fromEnvironment('MOUNTAYA_PUBLISHABLE_KEY');

class TrailMap extends StatefulWidget {
  const TrailMap({super.key});

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

class _TrailMapState extends State<TrailMap> {
  final mountaya = Mountaya(credential: .publishable(key: publishableKey));
  late final map = MapEngine(mountaya);

  @override
  void initState() {
    super.initState();
    unawaited(_show());
  }

  Future<void> _show() async {
    await map.setTerrain();
    await map.setItinerary(
      '0b6d1a4e-6d3f-4f7a-9c2e-1f0a5b8c3d21',
      fit: const CameraFit(padding: PixelPadding.all(48)),
    );
  }

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

  @override
  Widget build(BuildContext context) => MapView(engine: map);
}

Run it

flutter run --dart-define-from-file=.env

The map opens on the Tour du Mont-Blanc, with the Mountaya watermark in its top-left corner.

Next steps

Last updated