Skip to content

Simulate Navigation

Calculate a route and simulate navigating on it.

Simulate Navigation

Use case

Calculate a route between two given pairs of coordinates then simulate navigation on it.

How to use the sample

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

Download the Maps & Navigation SDK for C++ archive file for Linux or Windows

When you open the sample, you’ll be viewing the scene from above. When the route calculation is completed a simulation will start.

How it works

  1. Create an instance of Environment and set your API key token:

1Environment& env = Environment::GetInstance();
2
3//
4// Project API token available at:
5// https://developer.magiclane.com/api/projects
6//
7std::string projectApiToken = ""; //YOUR_TOKEN
  1. The SDK is initialized with your API key token string and the log file path, where to write the application logs. Note that logFilePath is not initialized by default, which means that no logs are written. The logFilePath is initialized with the first command line argument, if any.

1std::string logFilePath;
2if ( argc > 1 )
3   logFilePath = std::string(argv[1]);
4
5env.InitSDK( projectApiToken, logFilePath.c_str() );
  1. Create a MapViewListener, OpenGLContext and MapView.

1MapViewListenerImpl listener;
2auto oglContext = env.ProduceOpenGLContext("Simulation");
3gem::StrongPointer<gem::MapView> mapView = gem::MapView::produce(oglContext, &listener);
  1. Create a RouteList, a ProgressListener, a LandmarkList with two Landmarks in it and a RoutePreferences object. A route consists of at least 2 landmarks/waypoints, where the first one in the list is the departure position and the last one is the destination position, optionally with 0 or more intermediate waypoints.

1gem::RouteList routes;
2{
3    ProgressListener calculateRouteListener;
4    gem::LandmarkList waypoints;
5    waypoints.push_back(gem::Landmark("San Francisco", { 37.77903, -122.41991 }));
6    waypoints.push_back(gem::Landmark("San Jose", { 37.33619, -121.89058 }));
7    gem::RoutePreferences preferences;
  1. Call the RoutingService using RouteList, LandmarkList, RoutePreferences and the progress listener. Compute the route between the departure and destination given above, and wait up to 15 seconds.

1    gem::RoutingService().calculateRoute(routes, waypoints, preferences, &calculateRouteListener);
2    auto ret = WAIT_UNTIL(std::bind(&ProgressListener::IsFinished, &calculateRouteListener), 15000);
3}
  1. Once the route calculation operation completes, if there is at least one route in the resulting list of possible routes, add the first calculated route (the one at index 0) to the MapViewPreferences routes collection.

1if (routes.size() > 0)
2{
3    mapView->preferences().routes().add(routes[0], true);
  1. Create a ProgressListener and a NavigationListener. Instruct the NavigationService to start a simulation using the first route and the newly created listeners.

1    gem::NavigationListener navlistener;
2    ProgressListener navProgressListener;
3    gem::NavigationService().startSimulation(routes[0], navlistener, &navProgressListener);
  1. After 1 second (1000 milliseconds), instruct the MapView to start following the position. This will move the camera behind the green arrow simulating the vehicle position traveling along the selected route. Wait for the user to close the application window.

1    WAIT_TIME_OUT(1000);
2    mapView->startFollowingPosition();
3}
4WAIT_UNTIL_WINDOW_CLOSE();

C++ Examples

Maps SDK for C++ Examples can be downloaded or cloned with Git