Traffic and directions
The traffic and directions example is a visualization of Mapbox's traffic data and Directions API.
In this example scene, traffic is visualized by leveraging the Mapbox Traffic v1 tileset, which provides constantly updating congestion information. The tileset is specified in the Map Layers settings as a
Custom Data Source as shown in the screenshot below. You can specify multiple tilesets as data sources by separating them with a comma. For more information refer to the documentation on using custom data in Unity.
Map Features are used to render the congestion data inside of Unity. Navigate to
Map Layers > Features > Map Features to see how the visualizers are configured. The Mapbox Traffic v1 tileset gives access to layers and fields that are used for this visualization. The
traffic layer of the tileset contains the
congestion key which has
severe as possible values. Each one of these keys are configured with a separate visualizer so that it can be styled and rendered appropriately. To see how the vector features interact with tileset data, inspect the LowTrafficCongestion feature.
Select LowTrafficCongestion to view the configuration and you'll see the following settings:
- View the configured filters by navigating to Filters.
- Key is set to
- Operator is set to
- Str Value is set to
- Key is set to
- Primitive Type is set to
- Layer Name is set to
- Snap to Terrain is checked and Combine Meshes is unchecked.
Styling of this feature is handled in Behavior Modifiers by the following modifiers:
- Mesh Modifier: A LoftModifier is specified to achieve a 3D look.
- Game Object Modifier: A MaterialModifier is specified to color the area with low congestion a specific color.
Mapbox Navigation is available inside of Unity, and is configured using the DirectionsFactor.cs script. In this scene there is a
Directions game object that has the DirectionsFactory script attached to it. Additionally, it has two waypoints as children that are the markers on the map. Every two seconds the DirectionsFactory will query the Directions API and return the driving route between the two points. If the points have moved within that time, a line will be rendered along the new driving route.
The DirectionsFactory script contains the following:
- Map: This field contains the map game object for this scene.
- Mesh Modifiers: This is an array of mesh modifiers. It contains a LoftModifier to place a line on top of the traffic mesh.
- Material: This contains the material to style the line along the directions route.
- Waypoints: This array contains the waypoints of the route.
- Update Frequency: This setting controls how often the Directions API is queried and updates the route if the position of the waypoints have changed.
To configure directions in your own scene, you can drag the Directions prefab into a new scene or attach the DirectionsFactory script to an empty game object.