Skip to main content
APIs 6 min read

How to Reduce Your Carbon Footprint with Emissions-Aware Routing

CO2 emissions

In this blog post, we’ll explore sustainable transportation using HERE Routing API. We will demonstrate how to increase efficiencies in transportation by calculating CO2 emissions and fuel consumption for different routes. Cutting down on carbon emissions and fuel usage in how we get around isn’t just a choice anymore. It’s now a must to ensure that we are passing on a cleaner and cooler planet for our future generations. 

 

Understanding ‘fuel’ parameters 

In HERE Routing API, the fuel parameters are used for calculating consumption and related CO2 emission.  

  • First, the type attribute is required, specifying the vehicle’s fuel type – whether it’s diesel, petrol, LPG or another variant. Note that hybrid (EV + other fuel types) vehicles are not supported.  
  • Next, the freeFlowSpeedTable which is like a graph that shows how much fuel is used at different speeds. It is a list of speeds and their corresponding fuel consumption values. The graph smoothly connects the dots between the lowest and highest speeds to give a complete picture of how fuel is consumed.  
  • trafficSpeedTable allows for further fine-tuning the consumption calculation by modeling consumption for speeds in traffic conditions.
  • additionalConsumption accounts for miscellaneous fuel usage by the vehicle for any other reason than driving power, like air-conditioning for example.  
  • ascent factors in additional consumption for going up slopes.

 

Using the HERE Routing API to Calculate CO2 Emissions and Fuel Consumption 

With the fuel parameters mentioned above, we will construct a HERE Routing API request for a diesel car traveling from San Francisco, CA to North Lake Tahoe, CA.  

Copied
        https://router.hereapi.com/v8/routes? 
    origin=37.77712,-122.41966&
    destination=39.23738,-120.02814&
    return=summary,polyline&
    transportMode=car&
    fuel[type]=diesel&
    fuel[freeflowspeedtable]=10,0.09,20,0.07,30,0.06,40,0.05,60,0.04,130,0.06&
    fuel[trafficspeedtable]=10,0.09,20,0.07,50,0.06,110,0.07,130,0.08&
    fuel[additionalconsumption]=0.2&
    fuel[ascent]=0.1&
    &apikey={{REST_API_KEY}}
  

In the API response summary, we see CO2 emissions and fuel consumption (in liters) included for each section of the route.  

Copied
        {
    "routes": [
        {
            "id": "...",
            "sections": [
                {
                    "id": "...",
                    "type": "vehicle",
                    "departure": { 
                       ... TIME, LOCATION
                    },
                    "arrival": { 
                       ... TIME, LOCATION
                    },
                    "summary": {
                        "duration": 12067,
                        "length": 321549,
                        "consumption": 20.3138,
                        "baseDuration": 11080,
                        "co2Emission": 53.628
                    },
                    "polyline": "...",
                    "transport": {
                        "mode": "car"
                    },
                    "consumptionType": "diesel"
                }
            ]
        }
    ]
} 
  

Displaying Emissions-aware Route with HERE Maps API for JavaScript  

Now, let’s visualize this route and access the consumption values with HERE Maps API for JavaScript. We started with HERE platform account to obtain the API key for authentication. Next step is instantiating the map object to set the map type, zoom and center location. Then we set the routing parameters we used above; and define callback functions which is called if the routing request is successful. We get an instance of the router and calculate the route with these parameters. 

Copied
        // Initialize the platform object: 
var platform = new H.service.Platform({
    'apikey': ‘YOUR_API_KEY’
});

var origin = { lat: 37.77712, lng: -122.41966 };
var destination = { lat: 39.23738, lng: -120.02814 };

// Obtain the default map types from the platform object 
var maptypes = platform.createDefaultLayers();

// Instantiate (and display) a map object: 
var map = new H.Map(
document.getElementById('mapContainer'),
    maptypes.vector.normal.map,
    {
        zoom: 12,
        center: origin
    });

// Create the default UI: 
var ui = H.ui.UI.createDefault(map, maptypes, 'en-US');

// Enable the event system on the map instance: 
var mapEvents = new H.mapevents.MapEvents(map);

// Add event listener: 
map.addEventListener('tap', function (evt) {
    console.log(evt.type, evt.currentPointer.type);
});

// Instantiate the default behavior, providing the mapEvents object: 
var behavior = new H.mapevents.Behavior(mapEvents);

// Create the parameters for the routing request: 
var routingParameters = {
    'routingMode': 'fast',
    'transportMode': 'car',
    'origin': '37.77712,-122.41966',
    'destination': '39.23738,-120.02814',
    'fuel[type]': 'diesel',
    'fuel[freeflowspeedtable]': '10,0.09,20,0.07,30,0.06,40,0.05,60,0.04,130,0.06',
    'fuel[trafficspeedtable]': '10,0.09,20,0.07,50,0.06,110,0.07,130,0.08',
    'fuel[additionalconsumption]': '0.2',
    'fuel[ascent]': '0.1',
    'return': 'summary,polyline'
};

// Define a callback function to process the routing response 
var onResult = function (result) {
    console.log(result)
    // ensure that at least one route was found 
    if (result.routes.length) {
        result.routes[0].sections.forEach((section) => {
            // Create a linestring to use as a point source for the route line 
            let linestring = H.geo.LineString.fromFlexiblePolyline(section.polyline);

            // Create a polyline to display the route: 
            let routeLine = new H.map.Polyline(linestring, {
                style: { strokeColor: 'blue', lineWidth: 3 }
            });

            // Create a marker for the start point: 
            let startMarker = new H.map.Marker(section.departure.place.location);

            // Create a marker for the end point: 
            let endMarker = new H.map.Marker(section.arrival.place.location);

            // Add the route polyline and the two markers to the map: 
            map.addObjects([routeLine, startMarker, endMarker]);

            let fuelconsumption = (section.summary.consumption * 0.264172).toFixed(2); 
            //convert liters to US Gallons 
            let co2Emission = section.summary.co2Emission.toFixed(2);

            // Create an info bubble object at a specific geographic location:
            var bubble = new H.ui.InfoBubble(destination, {
                content: '<b>Fuel Consumption: </b>' + fuelconsumption + ' gallons' + '<br><br><b>CO2 Emissions: </b>' + co2Emission
            });
            ui.addBubble(bubble);

            // Set the map's viewport to make the whole route visible: 
            map.getViewModel().setLookAtData({ bounds: routeLine.getBoundingBox() });
        });
    }
};

// Get an instance of the routing service version 8: 
var router = platform.getRoutingService(null, 8);
router.calculateRoute(routingParameters, onResult,
    function (error) {
        alert(error.message);
    }); 
  

The following map shows the result of the route from San Francisco to Lake Tahoe. The pop-up shows fuel consumption (in gallons) and carbon emissions for a diesel-powered vehicle taking this route.

CO2 Emission JS

Fuel Consumption and CO2 emissions calculated by HERE Routing API in a HERE Maps API for JavaScript web application

 

Resources 

Mohini Todkari

Mohini Todkari

Sr. Developer Evangelist

Have your say

Sign up for our newsletter

Why sign up:

  • Latest offers and discounts
  • Tailored content delivered weekly
  • Exclusive events
  • One click to unsubscribe