Navigation simulation with on-demand map download¶
In this guide you will learn how to simulate online navigation along a route while downloading map data on demand.
Setup¶
First, get an API key token, see the Getting Started guide.
Download the Maps & Navigation SDK for Android archive fileDownload the DownloadingOnboardMapSimulation 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
Simulates online navigation along a route while downloading map data on demand.
How it works¶

You can open the MainActivity.kt file to see how the route computation and simulated navigation are set up.
1<uses-permission android:name="android.permission.INTERNET" />
Note that, in AndroidManifest.xml
, the INTERNET permission is
present, as this example downloads online map data on demand.
Downloaded map data is stored on the device in a directory such as this:
/sdcard/Android/data/com.generalmagic.examplename/files/Data/Maps/
.
In the class MainActivity : AppCompatActivity()
,
an instance of each of the following is created:
a navigation listener val navigationListener: NavigationListener
a progress listener for the route computation val routingProgressListener
a content listener to download required maps val contentListener
and the override fun onCreate()
to instantiate and initialize the
various panels used for computing the route and for navigation along
the route, and then it calls startSimulation()
.
1val contentListPair = contentStore.getStoreContentList(EContentType.RoadMap)
2?: return@execute
3for (map in contentListPair.first) {
4val mapName = map.name ?: continue
5if (mapName.compareTo(MAP_NAME, true) != 0)
6// searching another map
7 continue
8if (map.isCompleted())
9// already downloaded
10 return@execute
11// Define a listener to the progress of the map download action.
12val downloadProgressListener = ProgressListener.create(
13 onStarted = {
14 progressBar.visibility = View.VISIBLE
15 showToast("Started downloading $mapName.")
16 },
17 onCompleted = { errorCode, _ ->
18 progressBar.visibility = View.GONE
19 if (errorCode == GemError.NoError) {
20 showToast("$mapName was downloaded.")
21 onOnboardMapReady()
22 }
23 }
24)
25// Start downloading the first map item.
26map.asyncDownload(
27 downloadProgressListener,
28 GemSdk.EDataSavePolicy.UseDefault,
29 true
30)
31break
32}
In the contentListener()
val, we have the above check, which looks at the
online maps available on the server, to make sure that
the map required for the location where the route is to be computed or where
simulated navigation is to take place, is available on the server,
if not already downloaded, and downloads it.
1val localMaps =
2 contentStore.getLocalContentList(EContentType.RoadMap)
3 ?: return@onSdkInitSucceeded
4var mapFound = false
5for (map in localMaps) {
6 val mapName = map.name ?: continue
7 if (mapName.compareTo(MAP_NAME, true) != 0)
8 // searching another map
9 continue
10 if
11 (!map.isCompleted()) {
12 // can't continue with incomplete map.
13 break
14 }
15 mapFound = true
16}
17// Defines an action that should be done when the the sdk had been loaded.
18if (mapFound)
19 onOnboardMapReady()
In the onCreate()
function, we have the above check, which looks at the
local/downloaded/offline maps, to make sure that
the map required for the location where the route is to be computed or where
simulated navigation is to take place, is already downloaded, or if it is
still in the process of downloading, then give it more time so it can finish.
1private fun startSimulation() = SdkCall.execute {
2 val waypoints = arrayListOf(
3 Landmark("Luxembourg", 49.61588784436375, 6.135843869736401),
4 Landmark("Mersch", 49.74785494642988, 6.103323786692679)
5 )
6 navigationService.startSimulation(
7 waypoints,
8 navigationListener,
9 routingProgressListener
10 )
11 }
The startSimulation()
function first instantiates a list of 2 waypoints,
one for the departure location and one for the destination;
there can be more than 2 waypoints in a route, but not less than 2.
These 2 waypoints are hardcoded here, and the locations can be anywhere on
the planet, as the required country map will be downloaded.
fun Route.getEta()
function to get the estimated time of arrival for this route.fun Route.getRtt()
function to get the route time for this route.fun Route.getRtd()
function to get the route distance for this route.