> For the complete documentation index, see [llms.txt](https://docs.mapbox.com/android/navigation/v2/llms.txt)

# 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`](https://docs.mapbox.com/android/navigation/v2/api/2.22.2/libnavui-speedlimit/com.mapbox.navigation.ui.speedlimit.view/-mapbox-speed-limit-view/), displays the speed limit of the road the user is traveling on.

![A screen shot showing the speed limit component.](https://docs.mapbox.com/android/assets/ideal-img/navigation-examples-speed-limit-nav-ui-sdk.480.jpg)

> **Related content (example): [Render speed limit for a route](https://docs.mapbox.com/android/navigation/v2/examples/show-speed-limit/)**
> 
> Render the speed limit of the current road using the Speed Limit API and `MapboxSpeedLimitView`.

### 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
<?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`](https://docs.mapbox.com/android/navigation/v2/api/2.22.2/libnavui-speedlimit/com.mapbox.navigation.ui.speedlimit.api/-mapbox-speed-limit-api/).

```kotlin
// 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 powered by the [`LocationObserver`](https://docs.mapbox.com/android/navigation/v2/api/2.22.2/libnavigation-core/com.mapbox.navigation.core.trip.session/-location-observer/). Create an observer in your activity or fragment and register it with [`MapboxNavigation`](https://docs.mapbox.com/android/navigation/v2/api/2.22.2/libnavigation-core/com.mapbox.navigation.core/-mapbox-navigation/). Be sure to unregister the `LocationObserver` in either `onStop` or `onDestroy` to avoid leaking resources.

```kotlin
private val locationObserver = object : LocationObserver {

    override fun onNewRawLocation(rawLocation: Location) {
        ...
    }

    override fun onNewLocationMatcherResult(locationMatcherResult: LocationMatcherResult) {
        val value = speedLimitApi.updateSpeedLimit(locationMatcherResult.speedLimit)
        binding.speedLimitView.render(value)
    }
}
```

> **Note: 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`.