Documentation‎ > ‎

Adding Traffic Layer

JxMaps allows you to add real-time traffic information (for locations supported by Google Maps) to your maps using the TrafficLayer object. Traffic information is provided for the time at which the request is made. 

To add Traffic Layer to your map:

1. Create an instance of the Map object.
2. Set up the map properties.
3. Create an instance of the TrafficLayer and associate it with the created map.

The following example demonstrates how to add TrafficLayer to the map:
public class MapExample extends MapView {

public static void main(String[] args) {
MapView mapView = new MapView();
// Setting a ready handler to a MapView object. onMapReady will be called when the map initialization is done and
// the map object is ready to use. Current implementation of onMapReady customizes the map object.
mapView.setOnMapReadyHandler(new MapReadyHandler() {
@Override
public void onMapReady(MapStatus status) {
// Check if the map is loaded correctly
if (status == MapStatus.MAP_STATUS_OK) {
// Getting the associated map object
final Map map = mapView.getMap();
// Creating a map options object
MapOptions options = new MapOptions();
// Creating a map type control options object
MapTypeControlOptions controlOptions = new MapTypeControlOptions();
// Changing position of the map type control
controlOptions.setPosition(ControlPosition.TOP_RIGHT);
// Setting the map type control options
options.setMapTypeControlOptions(controlOptions);
// Setting the map options
map.setOptions(options);
// Setting the map center
map.setCenter(new LatLng(34.04924594193164, -118.24104309082031));
// Setting initial zoom value
map.setZoom(13.0);
// Enabling the traffic layer on the map
new TrafficLayer(map);
}
}
});


JFrame frame = new JFrame("Map Integration");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(mapView, BorderLayout.CENTER);
frame.setSize(700, 500);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
When you run this example you'll get the following as output:

Traffic Layer can also be configured via the TrafficLayerOptions class. In the current version, this class has only one property "autoUpdate" that controls if traffic information should be updated automatically.