Mapbox Navigation Observer
interface MapboxNavigationObserver
Content copied to clipboard
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)
}
}