Skip to content

GPX Route Simulation

In this guide you will learn how to load a GPX route, render it on the map, and then simulate navigation along that route.

Setup

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

Download the Maps & Navigation SDK for Android archive file

Download the GPXRouteSimulation project archive file or clone the project with Git

See the Configure Android Example guide.

Run the example

In Android Studio, from the File menu, select Sync Project with Gradle Files

An android device should be connected via USB cable.
Press SHIFT+F10 to compile, install and run the example on the android device.

GPX example Android screenshot

GPX example Android screenshot

GPX example Android screenshot

A GPX route is loaded and rendered on the map. Navigation along the GPX route is simulated. If a pan occurs, a button appears in the lower right corner of the screen. Pressing this button resumes following/tracking the green navigation simulation arrow along the route.

How it works

Android example screenshot

You can open the MainActivity.kt file to see how the GPX route is imported and rendered on the map.

In the class MainActivity : AppCompatActivity(), an instance of
val routingService = RoutingService() is created.
The override fun onCreate() calls the calculateRouteFromGPX() function after the map is loaded.
 1private fun calculateRouteFromGPX() = SdkCall.execute {
 2     val gpxAssetsFilename = "gpx/test_route.gpx"
 3
 4     // Opens GPX input stream.
 5     val input = applicationContext.resources.assets.open(gpxAssetsFilename)
 6
 7     // Produce a Path based on the data in the buffer.
 8     val track = Path.produceWithGpx(input/*.readBytes()*/) ?: return@execute
 9
10     // Set the transport mode to bike and calculate the route.
11     routingService.calculateRoute(track, ERouteTransportMode.Bicycle)
12}
This function defines the name of the GPX route file name as "gpx/test_route.gpx" which is found in the assets/ directory of this example.
Next, the file is opened, and a route is generated from the GPX data.
track = Path.produceWithGpx(input)
The routing service is used to calculate the route for bicycles.
routingService.calculateRoute(track, ERouteTransportMode.Bicycle)

Inside the val routingService = RoutingService(), the simulation is started automatically when the route computation is completed, using the first (index 0) route routes[0] in the resulting route list:

 1GemError.NoError -> {
 2    val route = routes[0]
 3    SdkCall.execute {
 4        navigationService.startSimulationWithRoute(
 5            route,
 6            navigationListener,
 7            routingProgressListener
 8        )
 9    }
10}
The purpose of the val navigationListener: NavigationListener is to detect when a user-driven pan of the map occured.
 1private val navigationListener: NavigationListener
 2= NavigationListener.create(
 3    onNavigationStarted = {
 4        SdkCall.execute {
 5            gemSurfaceView.mapView?.let { mapView ->
 6                mapView.preferences?.enableCursor = false
 7                navRoute?.let { route ->
 8                    mapView.presentRoute(route)
 9                }
10                enableGPSButton()
11                mapView.followPosition()
12            }
13        }
14    }
15)
In that case, the camera no longer follows/tracks the green navigation simulation arrow, and a “GPS” button becomes visible in the lower right corner of the screen.
When the user presses this button, the button disappears and the camera resumes following/tracking the green navigation simulation arrow.

Android Examples

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