Calculate Route¶
Calculate a route and show it on map.

Use case¶
Calculate a route between two given pairs of coordinates then display it on map.
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 WindowsWhen you open the sample, you’ll be viewing the scene from above. A fly will be performed to the calculated route.
How it works¶
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
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() );
Create a
MapViewListener
,OpenGLContext
andMapView
.
1MapViewListenerImpl listener;
2auto oglContext = env.ProduceOpenGLContext("calculateRoute");
3gem::StrongPointer<gem::MapView> mapView = gem::MapView::produce(oglContext, &listener);
Create a
RouteList
, aProgressListener
, aLandmarkList
with twoLandmarks
in it and aRoutePreferences
object.
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;
Call the
RoutingService
usingRouteList
,LandmarkList
,RoutePreferences
and the progress listener. Wait up to 15 seconds (15000 milliseconds) for the route computation to complete.
1 gem::RoutingService().calculateRoute(routes, waypoints, preferences, &calculateRouteListener);
2 auto ret = WAIT_UNTIL(std::bind(&ProgressListener::IsFinished, &calculateRouteListener), 15000);
3}
Once the route calculation operation completes, if the resulting route list contains at least one route, add the first calculated route (at index 0) to the
MapViewPreferences
routes collection. Instruct theMapView
to center on the first route with a 2 second animation.
1if (routes.size() > 0)
2{
3 mapView->preferences().routes().add(routes[0], true);
4 mapView->centerOnRoute(routes[0], gem::Rect(), gem::Animation(gem::AnimationLinear, gem::ProgressListener(), 2000));
5 auto ret = WAIT_UNTIL(std::bind(&MapViewListenerImpl::IsFinished, &listener), 15000);
6}