Route replay
Testing and previewing the navigation experience is an important part of building with the Navigation SDK. For example, you may want to investigate how turn-by-turn navigation performed during test rides as you develop, test, and demo your work. You can use the SDK's route replay functionality to go back in time and replay real rides.
Implement replay
The Navigation SDK's replay framework is comprised of two core classes:
MapboxReplayer
handles shifting historical event timestamps into real-time events.ReplayEventBase
is an interface for anything with a time unit. The time basis can be months in the past, in the future, or starting at0.0
.
There are also several helper classes you can use to create and listen to replay events:
ReplayHistoryMapper
converts history files into replay events.ReplayLocationEngine
extends the Android system'sLocationEngine
class. It observes theReplayEventUpdateLocation
and passes the location toMapboxNavigation
.ReplayRouteMapper
converts aDirectionsResponse
into replay events.ReplayProgressObserver
observesMapboxNavigation
and simulates route progress.
Record and use history files
Record a history file
To record a trip as a history file:
-
(Optional) Specify history file location using
HistoryRecorderOptions
:mapboxNavigation = MapboxNavigation(
NavigationOptions.Builder(this)
...
.historyRecorderOptions(
HistoryRecorderOptions.Builder()
.fileDirectory(myDirectory)
.build()
)
...
.build()
)You can skip this step, then the default location (
<app_directory>/files/mbx_nav/history
) will be used. -
Start recording:
mapboxNavigation.historyRecorder.startRecording()
-
Stop recording and the history file will be available in the path returned:
mapboxNavigation.historyRecorder.stopRecording { filePath ->
if (filePath != null) {
// move to a different path or upload to the cloud
}
}
// optionally, immediately start the next recording session outside of the callback:
mapboxNavigation.historyRecorder.startRecording()
Retrieve history files
There are two ways to retrieve a recorded history file to be used on a device:
- Store the JSON file in a directory and read the file. The Navigation SDK test app's
ReplayHistoryActivity
example uses this approach. The file is stored in the app's assets folder. - Host history files somewhere in a cloud-based backend database (for example Firebase, Amazon Web Services, or GitHub Pages).
Replay history data
After you've retrieved the history file, you can replay the history data using MapboxReplayer
:
- Map the history string to replay events using
ReplayHistoryMapper
. - Push the events into the
MapboxReplayer.pushEvents(..)
. - Substitute your
LocationEngine
with aReplayLocationEngine
. - Call
MapboxReplayer.play()
.
- When done call
MapboxReplayer.finish()
to stop and clean up the replayer.
Replay history using real data by reading a history string from a file and replaying it with the Navigation SDK.
Create custom events
You can create custom events and play them on their own or play them through history files.
Replay custom events
You can replay any event to test on its own (without a history file), for example a map gesture event or zooming in and out of the map.
Start by setting up a custom event that implements ReplayEventBase
. ReplayEventBase
is a base interface event that can be implemented with any event you want to test:
private data class ReplayEventInitialRoute(
override val eventTimestamp: Double,
val coordinates: List<LatLng>
) : ReplayEventBase
Then, set up the MapboxReplayer
and ReplayHistoryMapper
classes:
val mapboxReplayer = MapboxReplayer()
val replayHistoryMapper = ReplayHistoryMapper(customEventMappper, logger)
Create a ReplayHistoryLocationEngine
object and give it to MapboxNavigation
via the NavigationOptions
when you build a MapboxNavigation
object. This way, MapboxNavigation
will use the Location
objects:
val navigationOptions = NavigationOptions.Builder(context)
.accessToken("token")
.locationEngine(ReplayLocationEngine(mapboxReplayer))
.build()
mapboxNavigation = MapboxNavigation(navigationOptions)
Get history events:
val replayEvents = replayHistoryMapper.mapToReplayEvents(rideHistoryExampleJsonString)
Then, you can give the custom event to the MapboxReplayer
via MapboxReplayer.pushEvents()
:
mapboxReplayer.pushEvents(replayEvents)
The MapboxReplayer
also has a seekTo()
method function to move the player to timestamp that you want to play.
Finally, start replaying the events:
playReplay.setOnClickListener {
mapboxReplayer.play(context)
}
Replay custom events from history files
Navigation history files have several predefined events, which can be retrieved with the MapboxNavigation
class:
val historyString = mapboxNavigation.retrieveHistory()
Custom events can also be created and added to the history file:
mapboxNavigation.addHistoryEvent(eventTypeString, eventJsonProperties)
You must create a custom class that implements the Navigation SDK's CustomEventMapper
interface to replay the history file's custom events.
private class ReplayCustomEventMapper : CustomEventMapper {
override fun map(eventType: String, event: LinkedTreeMap<*, *>): ReplayEventBase? {
return when (eventType) {
"initial_route" -> {
val properties = event["properties"] as LinkedTreeMap<*, *>
val routeOptions = properties["routeOptions"] as LinkedTreeMap<*, *>
val coordinates = routeOptions["coordinates"] as List<List<Double>>
val coordinatesLatLng = coordinates.map { LatLng(it[1], it[0]) }
ReplayEventInitialRoute(
eventTimestamp = event["event_timestamp"] as Double,
coordinates = coordinatesLatLng
)
}
else -> null
}
}
}