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

equals
Link copied to clipboard
open operator fun equals(other: Any?): Boolean
hashCode
Link copied to clipboard
open fun hashCode(): Int
onAttached
Link copied to clipboard
abstract fun onAttached(mapboxNavigation: MapboxNavigation)
Signals that the mapboxNavigation instance is ready for use.
onDetached
Link copied to clipboard
abstract fun onDetached(mapboxNavigation: MapboxNavigation)
Signals that the mapboxNavigation instance is being detached.
toString
Link copied to clipboard
open fun toString(): String