MapboxNavigationObserver

interface MapboxNavigationObserver

Defines an object that needs to interact with or observe MapboxNavigation. Use the MapboxNavigationApp singleton to register and unregister observers with MapboxNavigationApp.registerObserver and MapboxNavigationApp.unregisterObserver.

Example of observing locations with a view model

class MyViewModel : ViewModel() {
private val locationObserver = MyLocationObserver()

val location: LiveData<Location> = locationObserver.location.asLiveData()

init {
MapboxNavigationApp.register(locationObserver)
}

override fun onCleared() {
MapboxNavigationApp.unregister(locationObserver)
}
}

class MyLocationObserver : MapboxNavigationObserver {
private val mutableLocation = MutableStateFlow<LocationMatcherResult?>(null)
val locationFlow: Flow<LocationMatcherResult?> = mutableLocation

private val locationObserver = object : LocationObserver {
override fun onNewLocationMatcherResult(locationMatcherResult: LocationMatcherResult) {
mutableLocation.value = locationMatcherResult
}

override fun onNewRawLocation(rawLocation: Location) {
// no op
}
}

override fun onAttached(mapboxNavigation: MapboxNavigation) {
mapboxNavigation.registerLocationObserver(locationObserver)
}

override fun onDetached(mapboxNavigation: MapboxNavigation) {
mapboxNavigation.unregisterLocationObserver(locationObserver)
}
}

Functions

Link copied to clipboard
abstract fun onAttached(mapboxNavigation: MapboxNavigation)

Signals that the mapboxNavigation instance is ready for use. Use this function to register mapboxNavigation observers, such as MapboxNavigation.registerRoutesObserver.

Link copied to clipboard
abstract fun onDetached(mapboxNavigation: MapboxNavigation)

Signals that the mapboxNavigation instance is being detached. Use this function to unregister mapboxNavigation observers that were registered in onAttached.