Skip to content

Human Voices

This example demonstrates the functionalities of the map SDK, including route calculation, simulation of navigation, and voice instructions.

human_voices - example flutter screenshot

Setup

First, get an API key token, see the Getting Started guide.

Prerequisites

Make sure you completed the Environment Setup - Flutter Examples guide before starting this guide.

Build and Run

Go to the human_voices directory within the Flutter examples directory. This is the directory name for this example project.

human_voices - example flutter screenshot

Note - the gem_kit directory containing the Maps SDK for Flutter should be in the plugins directory of the example, e.g. example_pathname/plugins/gem_kit - see the environment setup guide above. Replace example_pathname with the actual example path name, such as address_search

Download project dependencies:

example flutter upgrade screenshot

flutter upgrade

example flutter clean screenshot

run the following terminal commands in the project directory, where the pubspec.yaml file is located:

flutter clean

example flutter pub get screenshot

flutter pub get

Run the example:

First, verify that the ANDROID_SDK_ROOT environment variable is set to the root path of your android SDK.

In android/build.gradle add the maven {} block as shown, within the allprojects {} block, for both debug and release builds, without the line numbers, those are for reference:

1allprojects {
2    repositories {
3        google()
4        mavenCentral()
5        maven {
6           url "${rootDir}/../plugins/gem_kit/android/build"
7        }
8    }
9}

in android/app/build.gradle within the android {} block, in the defaultConfig {} block, the android SDK version minSdk must be set as shown below.

Additionally, for release builds, in android/app/build.gradle, within the android {} block, add the buildTypes {} block as shown:

Replace example_pathname with the actual example pathname, such as center_coordinates

 1android {
 2    defaultConfig {
 3        applicationId "com.magiclane.gem_kit.examples.example_pathname"
 4        minSdk 21
 5        targetSdk flutter.targetSdk
 6        versionCode flutterVersionCode.toInteger()
 7        versionName flutterVersionName
 8    }
 9    buildTypes {
10        release {
11            minifyEnabled false
12            shrinkResources false
13
14            // TODO: Add your own signing config for the release build.
15            // Signing with the debug keys for now, so `flutter run --release` works.
16            signingConfig signingConfigs.debug
17        }
18    }
19}

Then run the project:

flutter run --debug
or
flutter run --release

In the ios/Podfile configuration text file, at the top, set the minimum ios platform to 13 like this:

platform :ios, '13.0'

Then run the project:

flutter run --debug
or
flutter run --release

How It Works

This example integrates several components to simulate navigation with voice instructions. Here’s a breakdown of the key functionalities:

human_voices - example flutter screenshot

Map Initialization

The GemKit SDK is initialized with the necessary API token. The GemMapController is used to manage the map and the displayed routes.

 1import 'package:gem_kit/core.dart';
 2import 'package:gem_kit/map.dart';
 3import 'package:gem_kit/navigation.dart';
 4import 'package:gem_kit/routing.dart';
 5
 6import 'bottom_navigation_panel.dart';
 7import 'top_navigation_panel.dart';
 8import 'tts_engine.dart';
 9import 'utility.dart';
10
11import 'package:flutter/material.dart' hide Route;
12
13Future<void> main() async {
14  const projectApiToken = String.fromEnvironment('GEM_TOKEN');
15
16  await GemKit.initialize(appAuthorization: projectApiToken);
17  runApp(const MyApp());
18}
19
20class MyApp extends StatelessWidget {
21  const MyApp({super.key});
22
23  @override
24  Widget build(BuildContext context) {
25    return const MaterialApp(
26      debugShowCheckedModeBanner: false,
27      title: 'Human Voices',
28      home: MyHomePage(),
29    );
30  }
31}

The MaterialApp serves as the root of the application, hosting the MyHomePage widget, where the map and navigation features are implemented.

Route Calculation

The user can trigger route calculation, which involves defining landmarks and preferences, then using RoutingService.calculateRoute to compute the route.

 1void _onBuildRouteButtonPressed(BuildContext context) {
 2  _showSnackBar(context, message: 'The route is calculating.');
 3
 4  // Define landmarks.
 5  final departureLandmark =
 6      Landmark.withLatLng(latitude: 48.87586, longitude: 2.30311);
 7  final intermediaryPointLandmark =
 8      Landmark.withLatLng(latitude: 48.87422, longitude: 2.29952);
 9  final destinationLandmark =
10      Landmark.withLatLng(latitude: 48.87361, longitude: 2.29513);
11
12  // Define the route preferences.
13  final routePreferences = RoutePreferences();
14
15  // Calculate the route.
16  _routingHandler = RoutingService.calculateRoute(
17      [departureLandmark, intermediaryPointLandmark, destinationLandmark],
18      routePreferences, (err, routes) {
19    _routingHandler = null;
20    ScaffoldMessenger.of(context).clearSnackBars();
21
22    if (err == GemError.success) {
23      final routesMap = _mapController.preferences.routes;
24      for (final route in routes!) {
25        routesMap.add(route, route == routes.first,
26            label: route.getMapLabel());
27      }
28      _mapController.centerOnRoutes(routes: routes);
29      setState(() => _areRoutesBuilt = true);
30    }
31  });
32}

Simulated Navigation

Once the route is built, the user can start the navigation simulation. The simulation triggers text-to-speech (TTS) instructions using the TTSEngine class.

 1void _startSimulation() {
 2  final routes = _mapController.preferences.routes;
 3
 4  _navigationHandler = NavigationService.startSimulation(routes.mainRoute,
 5      (type, instruction) async {
 6    if (type == NavigationEventType.destinationReached ||
 7        type == NavigationEventType.error) {
 8      setState(() {
 9        _isSimulationActive = false;
10        _cancelRoute();
11      });
12      return;
13    }
14    _isSimulationActive = true;
15    if (instruction != null) {
16      setState(() => currentInstruction = instruction);
17    }
18  }, onTextToSpeechInstruction: (textInstruction) {
19    _ttsEngine.speakText(textInstruction);
20  }, speedMultiplier: 20);
21
22  _mapController.startFollowingPosition();
23}
The onTextToSpeechInstruction: callback receives the TTS instructions, which are then spoken using the TTSEngine.
The camera follows the simulated position as the simulation progresses.

UI Components

The example includes custom UI components, such as NavigationInstructionPanel and NavigationBottomPanel, to display navigation instructions and other relevant details during the simulation.

Clean-Up

The dispose() method ensures that resources are properly released when the widget is destroyed.

1@override
2void dispose() {
3  GemKit.release();
4  super.dispose();
5}

Flutter Examples

Maps SDK for Flutter Examples can be downloaded or cloned with Git