# Traffic

> **Note (legacy): Not compatible with the Maps SDK v10**
> 
> This plugin is not compatible with the Maps SDK v10 or higher. Learn more in the [Maps SDK for Android](https://docs.mapbox.com/android/maps/guides/) documentation.

The **Traffic Plugin for Android** adds a real-time traffic layer to any Mapbox base map. If you want to display a traffic layer inside your application, you only need to include the dependency in your project and initialize the plugin. Various shades of colors show the congestion level for any given part of a road segment. The plugin will not show road information if there's not enough traffic data available for a given road.

Like other plugins, the traffic plugin's constructor can receive the ID of the layer in which you want the traffic to display below. If the layer below ID isn't in the constructor, the plugin will try to place the traffic below all `SymbolLayer`s so that text and icons on the map are still visible on top of the traffic lines. It is always a good idea to pass in a string ID rather than relying on the plugin to try to place the traffic below a `SymbolLayer` because it isn't guaranteed to work properly.

## Install the Traffic Plugin

To start developing an application using the Traffic Plugin, you'll need to add the appropriate dependencies inside your `build.gradle` file. This dependency includes the Maps SDK for Android. You can find all dependencies given on MavenCentral.

> **Note: Android's 64K method count limit**
> 
> If your application is over the 64K method limit, you can [shrink, obfuscate, and optimize your code](https://developer.android.com/studio/build/shrink-code) with R8 or ProGuard. If those steps do not lower your method count below 64K, you can also [enable multidex](https://developer.android.com/studio/build/multidex).

### Add the dependency

1.  Start Android Studio.
2.  Open up your application's `build.gradle`.
3.  Make sure that your project's `minSdkVersion` is API 14 or higher.
4.  Under dependencies, add a new build rule for the latest `mapbox-android-plugin-traffic-v9`.
5.  Click the Sync Project with Gradle Files near the toolbar in Studio.

```groovy
repositories {
  mavenCentral()
}

dependencies {
  implementation 'com.mapbox.mapboxsdk:mapbox-android-plugin-traffic-v9:0.10.0'
}
```

## Add traffic

It's necessary to initialize the plugin either inside `onStyleLoaded()` (recommended) or in another place you know the `MapboxMap`, `MapView`, and `Style` objects won't be null. Once initialized, passing true through `setVisibility()` will show the traffic layer on the map.

**Java**

```java

mapboxMap.setStyle(Style.MAPBOX_STREETS, new Style.OnStyleLoaded() {
  @Override
  public void onStyleLoaded(@NonNull Style style) {

    TrafficPlugin trafficPlugin = new TrafficPlugin(mapView, mapboxMap, style);

  }
});
```

**Kotlin**

```kt

mapboxMap.setStyle(Style.MAPBOX_STREETS) { style ->

    val trafficPlugin = TrafficPlugin(mapView, mapboxMap, style)
}
```

> **Related content (example): [Traffic plugin](https://docs.mapbox.com/android/legacy/maps/examples/location-component-options/)**
> 
> See how to install and use the Traffic Plugin.

## Check visibility

The plugin also has a visibility getter method to check whether the traffic layer is visible on the map.

**Java**

```java

boolean trafficShowingOnMap = trafficPlugin.setVisibility(true);
```

**Kotlin**

```kt

val trafficShowingOnMap = trafficPlugin.isVisible
```

## Traffic colors

The table below provides information for each color displayed in the plugin's traffic layer and what the corresponding congestion level is.

| Color | Hex value | Congestion level |
| --- | --- | --- |
| Green | `#39c66d` | Low |
| Yellow | `#ff8c1a` | Moderate |
| Orange | `#ff0015` | Heavy |
| Red | `#981b25` | Severe |