Simulate Route¶
Setup¶
Prerequisites¶
Build and run¶
Go to the simulate_route
directory,
within the flutter examples directory - that is the name of this example project.
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:
flutter upgrade
run the following terminal commands in the project directory,
where the pubspec.yaml
file is located:
flutter clean
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
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
flutter run --release
How it works¶
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 'utility.dart';
9
10import 'package:flutter/material.dart' hide Route, Animation;
The dart
material package is imported, as well as the
required gem_kit
packages.
1void _onMapCreated(GemMapController controller) {
2 // Save controller for further usage.
3 _mapController = controller;
4}
This callback function is called when the interactive map is initialized and ready to use.
1void _onBuildRouteButtonPressed(BuildContext context) {
2 // Define the departure.
3 final departureLandmark = Landmark.withLatLng(latitude: 45.6517672, longitude: 25.6271132);
4
5 // Define the destination.
6 final destinationLandmark = Landmark.withLatLng(latitude: 44.4379187, longitude: 26.0122374);
7
8 // Define the route preferences.
9 final routePreferences = RoutePreferences();
10 _showSnackBar(context, message: 'The route is calculating.');
11
12 // Calling the calculateRoute SDK method.
13 // (err, results) - is a callback function that gets called when the route computing is finished.
14 // err is an error enum, results is a list of routes.
15 _routingHandler =
16 RoutingService.calculateRoute([departureLandmark, destinationLandmark], routePreferences, (err, routes) async {
17 // If the route calculation is finished, we don't have a progress listener anymore.
18 _routingHandler = null;
19
20 ScaffoldMessenger.of(context).clearSnackBars();
21
22 // If there aren't any errors, we display the routes.
23 if (err == GemError.success) {
24 // Get the routes collection from map preferences.
25 final routesMap = _mapController.preferences.routes;
26
27 // Display the routes on map.
28 for (final route in routes!) {
29 routesMap.add(route, route == routes.first, label: route.getMapLabel());
30 }
31
32 // Center the camera on routes.
33 _mapController.centerOnRoutes(routes);
34 }
35 setState(() {
36 _areRoutesBuilt = true;
37 });
38 });
39}
When the route button in the upper right corner is pressed, a route is computed from the preset departure position to the preset destination position.
A route must have at least 2 waypoints, one for the departure, and one for the destination. Optionally, 0 or more intermediate waypoints may be specified, through which the route will pass, in order from departure to destination.
Landmark
and has latitude and longitude coordinates.RoutingService.calculateRoute()
routesMap.add()
_mapController.centerOnRoutes(routes);
1void _startSimulation() {
2 final routes = _mapController.preferences.routes;
3
4 _navigationHandler = NavigationService.startSimulation(routes.mainRoute, (type, instruction) async {
5 if (type == NavigationEventType.destinationReached || type == NavigationEventType.error) {
6 // If the navigation has ended or if and error occurred while navigating, remove routes.
7 setState(() {
8 _isSimulationActive = false;
9 _cancelRoute();
10 });
11 return;
12 }
13 _isSimulationActive = true;
14
15 if (instruction != null) {
16 setState(() => currentInstruction = instruction);
17 }
18 });
19
20 // Set the camera to follow position.
21 _mapController.startFollowingPosition();
22}
_mapController.startFollowingPosition();