Speed limit

The Mapbox Navigation SDK allows you to present speed limit information in your application using a pre-built UI component.

Use the default speed limit UI component

The default speed limit UI component, MapboxSpeedLimitView, displays the speed limit of the road the user is currently traveling on.

Add the view to the layout

Start by adding the MapboxSpeedLimitView to your activity or fragment layout. The sample code below positions the speed limit UI component on top of a map.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

  <com.mapbox.maps.MapView
      android:id="@+id/mapView"
      android:layout_width="0dp"
      android:layout_height="0dp"
      app:layout_constraintTop_toTopOf="parent"
      app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintBottom_toBottomOf="parent"
      />

  <com.mapbox.navigation.ui.speedlimit.view.MapboxSpeedLimitView
      android:id="@+id/speedLimitView"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginStart="@dimen/mapbox_dimen_8dp"
      android:layout_marginBottom="@dimen/mapbox_dimen_8dp"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintBottom_toBottomOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Create an instance of the Speed Limit API

The Speed Limit API formats speed limit data to be used in the speed limit UI component..

Create an instance of MapboxSpeedLimitApi.

// Define speed limit formatter options
private val speedLimitFormatter: SpeedLimitFormatter by lazy {
    SpeedLimitFormatter(this)
}
// Create an instance of the Speed Limit API
private val speedLimitApi: MapboxSpeedLimitApi by lazy {
    MapboxSpeedLimitApi(speedLimitFormatter)
}

Start receiving map matching events

The updates for the speed limit component are driven by the MapMatcherResultObserver. Create an observer in your activity or fragment and register it with MapboxNavigation. Be sure to unregister the MapMatcherResultObserver in either onStop or onDestroy to avoid leaking resources.

private val mapMatcherObserver = object : MapMatcherResultObserver {
    override fun onNewMapMatcherResult(mapMatcherResult: MapMatcherResult) {
        val value = speedLimitApi.updateSpeedLimit(mapMatcherResult.speedLimit)
        binding.speedLimitView.render(value)
    }
}
Speed limit data availability

Mapbox doesn't have speed limit data for every road. When the data is not available for a given road the value returned by the MapboxSpeedLimitApi will be Expected, and the displayed value will be "--". If it better suites your use case, you may decide to hide and show the speed limit component based on the value returned by the MapboxSpeedLimitApi.