Find traffic

You can visualize traffic incidents on the map by enabling the map layer state TRAFFIC_INCIDENTS with one line of code. The HERE SDK supports also a separate layer to see the current traffic situation by adding a TRAFFIC_FLOW layer.

In addition, you can also indicate the traffic along a Route instance as shown in the Directions section.

Show real-time traffic flow and incidents on the map

You can easily visualize traffic incidents on the map by enabling the map layer state TRAFFIC_INCIDENTS. The HERE SDK also supports a separate layer to see the current traffic situation. See the example below for how to show or hide a layer on the map.

Screenshot: Traffic incidents visualized on the map.

After a layer is set, the visible area of the map is automatically updated. So you can freely pan the map in all directions to see the latest traffic incidents.

In many situations, drivers are interested in finding the fastest route based on the current traffic jams in a city - or outside a city. The HERE SDK allows you to show a layer holding all the current traffic jams, visualized by lines in different colors to indicate the severity of the jam - always updated in real-time. This feature requires an online connection and consumes slightly more data. However, the traffic lines are shown as part of the map tiles and are therefore highly performant.

Together - or independently - you can visualize such traffic information on the map with just a few lines of code:

try {
    mapView.getMapScene().setLayerState(MapLayer.TRAFFIC_FLOW, LayerState.ENABLED);
    mapView.getMapScene().setLayerState(MapLayer.TRAFFIC_INCIDENTS, LayerState.ENABLED);
} catch (MapScene.MapSceneException e) {
    Toast.makeText(context, "Exception when enabling traffic visualization.", Toast.LENGTH_LONG).show();
}

Setting a new layer state is performed synchronously, but it requires a valid map scene that must have been loaded before. Also, setting a new layer state while a new map scene is being loaded, may result in an exception. For hiding a layer, you can call the above with LayerState.DISABLED instead of LayerState.ENABLED.

The traffic flow lines are color coded as follows:

  • Green: Normal traffic
  • Amber/Yellow: High traffic
  • Red: Very high traffic
  • Black: Blocking traffic

A usage example is available on GitHub as part of the Traffic example app.

Use MapSceneConfig to enable more than one layer together with a new map scene

Sometimes it is more convenient to load the map scene using a MapSceneConfig. For example, if you want to enable visualization for traffic incidents, traffic flows and a new map scene at the same time, call this:

MapSceneConfig mapSceneConfig = new MapSceneConfig();
mapSceneConfig.mapLayers.put(MapLayer.TRAFFIC_FLOW, LayerState.ENABLED);
mapSceneConfig.mapLayers.put(MapLayer.TRAFFIC_INCIDENTS, LayerState.ENABLED);
mapView.getMapScene().loadScene(MapStyle.NORMAL_DAY, mapSceneConfig, errorCode -> {
    if (errorCode != null) {
        Log.d(TAG, "loadScene failed: " + errorCode.toString());
    }
});

Here, the mapSceneConfig holds the desired layers, TRAFFIC_FLOW and TRAFFIC_INCIDENTS, together with their new state. When loading a new map scene, mapSceneConfig can be passed together with the desired map style as parameters.

For hiding both layers at the same time while setting a new map scene, you can call the above with LayerState.DISABLED instead of LayerState.ENABLED. Alternatively, you can independently control the state of a map layer using a synchronous call to setLayerState() as shown above.

Screenshot: Traffic flow visualized on the map together with incidents.

results matching ""

    No results matching ""