Get started

Turn-by-turn navigation

Navigation follows a route, tells where the traveller stands, and says each maneuver aloud. The machine is pure Dart in mountaya_geo; mountaya_navigation_flutter adds the device's location and voice.

Read the route

readItineraryRoute joins a saved itinerary's segments into the one route a traveller follows:

import 'package:mountaya_client/mountaya_client.dart';
import 'package:mountaya_geo/mountaya_geo.dart';

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

final mountaya = Mountaya(credential: .publishable(key: publishableKey));
final read = await mountaya.studio.itineraries.getItinerary(
  itinerary_id: '0b6d1a4e-6d3f-4f7a-9c2e-1f0a5b8c3d21',
);
final route =
    readItineraryRoute(read.data.segments, waypoints: read.data.waypoints) ??
    (throw StateError('The itinerary holds no line to follow.'));
print('${route.line.length} positions, ${route.steps.length} steps');
await mountaya.close();

Follow the route

RouteFollowingEngine follows a route on the positions you hand it, on a server or in a test:

import 'package:mountaya_client/mountaya_client.dart';
import 'package:mountaya_geo/mountaya_geo.dart';

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

final mountaya = Mountaya(credential: .publishable(key: publishableKey));
final navigation = RouteFollowingEngine<String>(
  // Your route source: the saved itinerary of each id, read as one route.
  route: (itineraryId, signal) async {
    final read = await mountaya.studio.itineraries.getItinerary(
      itinerary_id: itineraryId,
      options: RequestOptions(signal: signal),
    );
    return readItineraryRoute(read.data.segments, waypoints: read.data.waypoints) ??
        (throw StateError('The itinerary holds no line to follow.'));
  },
);
navigation.start('0b6d1a4e-6d3f-4f7a-9c2e-1f0a5b8c3d21');
await navigation.subscribe().firstWhere((state) => state.route != null);

// Each position you read, [longitude, latitude]: here, Chamonix.
navigation.setPosition([6.8712, 45.9231]);
print(navigation.state.status);
navigation.close();
await mountaya.close();

In a Flutter app, DeviceNavigationEngine is the same machine with the device's location and voice beside it.

Use the device's location

DeviceLocationSource reads the device's satellite position while your app holds location access:

import 'package:mountaya_client/mountaya_client.dart';
import 'package:mountaya_geo/mountaya_geo.dart';
import 'package:mountaya_navigation_flutter/mountaya_navigation_flutter.dart';

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

final mountaya = Mountaya(credential: .publishable(key: publishableKey));
final location = DeviceLocationSource(rate: LocationRate.walking);
final navigation = DeviceNavigationEngine<String>(
  route: (itineraryId, signal) async {
    final read = await mountaya.studio.itineraries.getItinerary(
      itinerary_id: itineraryId,
      options: RequestOptions(signal: signal),
    );
    return readItineraryRoute(read.data.segments, waypoints: read.data.waypoints) ??
        (throw StateError('The itinerary holds no line to follow.'));
  },
  location: location,
);
navigation.start('0b6d1a4e-6d3f-4f7a-9c2e-1f0a5b8c3d21');

// The app that made each closes it, the engine first.
navigation.close();
location.close();
await mountaya.close();

The plugin never asks for location access. Ask in your own flow, and declare it in ios/Runner/Info.plist:

<key>NSLocationWhenInUseUsageDescription</key>
<string>Your position along the route you follow.</string>

And in android/app/src/main/AndroidManifest.xml:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

Speak the prompts

SpeechNavigationVoice says your sentence for each instruction. An instruction is a key and its arguments, never a sentence:

import 'package:mountaya_geo/mountaya_geo.dart';
import 'package:mountaya_navigation_flutter/mountaya_navigation_flutter.dart';

final voice = SpeechNavigationVoice(
  language: 'fr-FR',
  // Your words for each instruction. One you answer null for is not said.
  words: (instruction) => switch (instruction.key) {
    NavigationInstructionKey.turnLeft => 'Tournez à gauche sur ${instruction.name}',
    NavigationInstructionKey.turnLeftPlain => 'Tournez à gauche',
    NavigationInstructionKey.waypoint => 'Passez par ${instruction.name}',
    NavigationInstructionKey.goal ||
    NavigationInstructionKey.goalPlain => 'Vous êtes arrivé',
    _ => null,
  },
);
await voice.prepare();
await voice.close();

Hand it to DeviceNavigationEngine(voice: voice). The engine says each maneuver once, 50 m before it.

Show progress

The engine's state carries the progress. Read it in a widget through a NavigationListenable:

import 'package:flutter/widgets.dart';
import 'package:mountaya_geo/mountaya_geo.dart';
import 'package:mountaya_navigation_flutter/mountaya_navigation_flutter.dart';

class NavigationBanner extends StatefulWidget {
  const NavigationBanner({super.key, required this.navigation});

  final NavigationEngine<String> navigation;

  @override
  State<NavigationBanner> createState() => _NavigationBannerState();
}

class _NavigationBannerState extends State<NavigationBanner> {
  static const _units = UnitPreferences(language: 'en');

  // Made once, never inside build, and disposed with the State.
  late final NavigationListenable _state = NavigationListenable(widget.navigation);

  @override
  void dispose() {
    _state.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) => ValueListenableBuilder<NavigationState>(
    valueListenable: _state,
    builder: (context, navigation, _) => switch (navigation.progress) {
      final progress? => Text(
        '${formatDistance(progress.toManeuver.distance, _units)} to the next turn, '
        '${formatDistance(progress.toArrival.distance, _units)} to go',
      ),
      null => Text(navigation.status.id),
    },
  );
}

Replay a route

ReplayLocationSource walks each route the engine follows at the speed you name, for a demo or a test:

import 'package:mountaya_client/mountaya_client.dart';
import 'package:mountaya_geo/mountaya_geo.dart';
import 'package:mountaya_navigation_flutter/mountaya_navigation_flutter.dart';

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

final mountaya = Mountaya(credential: .publishable(key: publishableKey));
final replay = ReplayLocationSource(metersPerSecond: 10);
final navigation = DeviceNavigationEngine<String>(
  route: (itineraryId, signal) async {
    final read = await mountaya.studio.itineraries.getItinerary(
      itinerary_id: itineraryId,
      options: RequestOptions(signal: signal),
    );
    return readItineraryRoute(read.data.segments, waypoints: read.data.waypoints) ??
        (throw StateError('The itinerary holds no line to follow.'));
  },
  location: replay,
);
navigation.start('0b6d1a4e-6d3f-4f7a-9c2e-1f0a5b8c3d21');
await navigation.subscribe().firstWhere(
  (state) => state.status == NavigationStatus.arrived,
);
navigation.close();
await mountaya.close();

Next steps

Last updated