With the Google Maps API V3 it’s very easy to make the built-in traffic overlay visible.

This is possible with the following code:

var options = {
    zoom: myDefaultZoom,
    center: new google.maps.LatLng(myLatitude, myLongitude),
    mapTypeId: google.maps.MapTypeId.ROADMAP

var map = new google.maps.Map(document.getElementById('map-container'), options);

var trafficLayer = new google.maps.TrafficLayer();

Too bad that there is no simple line of code to enable a built-in traffic toggle control (Buttons on the map are called controls). So I created the piece of code by myself to add a nice control called traffic like the “map” and “satellite” control. Please note that I used functionality of jQuery. The jQuery object is reference by the “$” character. But if you use another JavaScript library or want to use pure JavaScript, it won’t be to hard to tune the code a bit.

The end result will look like:

I hear you think “awesome”!!! Haha all right then, lets make it happen! Add the following JavaScript:

var controlDiv = document.createElement('DIV');
var controlUI = document.createElement('DIV');
var legend = '<ul>'
           + '<li><span style="background-color: #30ac3e">&nbsp;&nbsp;</span><span style="color: #30ac3e"> &gt; 80 km per hour</span></li>'
           + '<li><span style="background-color: #ffcf00">&nbsp;&nbsp;</span><span style="color: #ffcf00"> 40 - 80 km per hour</span></li>'
           + '<li><span style="background-color: #ff0000">&nbsp;&nbsp;</span><span style="color: #ff0000"> &lt; 40 km per hour</span></li>'
           + '<li><span style="background-color: #c0c0c0">&nbsp;&nbsp;</span><span style="color: #c0c0c0"> No data available</span></li>'
           + '</ul>';
var controlLegend = document.createElement('DIV');
// Set hover toggle event
    .mouseenter(function() {
    .mouseleave(function() {
var trafficLayer = new google.maps.TrafficLayer();
google.maps.event.addDomListener(controlUI, 'click', function() {
    if (typeof trafficLayer.getMap() == 'undefined' || trafficLayer.getMap() === null) {
    } else {

I used the following CSS to style the control to look just like the other controls:

.gmap-control-container {
    margin: 5px;
.gmap-control {
    cursor: pointer;
    background-color: -moz-linear-gradient(center top , #FEFEFE, #F3F3F3);
    background-color: #FEFEFE;
    border: 1px solid #A9BBDF;
    border-radius: 2px;
    padding: 0 6px;
    line-height: 160%;
    font-size: 12px;
    font-family: Arial,sans-serif;
    box-shadow: 2px 2px 3px rgba(0, 0, 0, 0.35);
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: none;
    -o-user-select: none;
    user-select: none;
.gmap-control:hover {
    border: 1px solid #678AC7;
.gmap-control-active {
    background-color: -moz-linear-gradient(center top , #6D8ACC, #7B98D9);
    background-color: #6D8ACC;
    color: #fff;
    font-weight: bold;
    border: 1px solid #678AC7;
.gmap-control-legend {
    position: absolute;
    text-align: left;
    z-index: -1;
    top: 20px;
    right: 0;
    width: 150px;
    height: 66px;
    font-size: 10px;
    background: #FEFEFE;
    border: 1px solid #A9BBDF;
    padding: 10px;
    box-shadow: 2px 2px 3px rgba(0, 0, 0, 0.35);
.gmap-control-legend ul {
    margin: 0;
    padding: 0;
    list-style-type: none;
.gmap-control-legend li {
    line-height: 160%;

The traffic control should be styled correct and toggling the traffic overlay nicely now. Enjoy!