Decoded

The vehicleHeading Routing Parameter Explained

Victor Ikechukwu
Jul 19, 2022 • Last edit on Jul 22, 20226 min read
Two laptops showing TomTom maps

Routing and Extended Routing APIs are services TomTom offers that enable developers to integrate and manage performant routes on their mapping applications.

The Routing API calculates and displays the most accurate routes between two locations — the origin and the destination — based on configurable parameters you set, such as the type of vehicle, the load it’s carrying, its departure time, and the driver’s ETA.

The same applies to the Extended Routing API. The difference between the Routing API and the Extended Routing API is that the vehicle to route using the latter must have an electric engine, as the route constructed by the service automatically has charging stops added to it based on the vehicle's consumption and charging model.

Both Routing and Extended Routing APIs accept certain parameters, such as the current traffic, avoidance conditions, departure, and arrival time, to construct the routes best suited for the vehicle’s condition. One of these parameters is vehicleHeading.

This article discusses the vehicleHeading parameter. It explores what it is, how it works, and how its value can affect the routes displayed by the Routing API.

What Is the vehicleHeading Parameter?

We can use the Routing API to calculate a route between two locations — for example, a delivery route from a restaurant to a customer's home address.

To calculate the route, we use the vehicleHeading parameter to represent the cardinal direction the vehicle is heading in to reach that location. The vehicleHeading parameter accepts a numerical value between 0 and 359 that represents the angle the vehicle is heading in degrees. Using vehicleHeading enables the TomTom Routing API to construct a route that’s most suitable for the location’s geographical position.

How Does the vehicleHeading Parameter Work?

To demonstrate how the vehicleHeading parameter works and how it can affect the routes displayed by the Routing API, let’s set up a React application to see vehicleHeading in action. The app displays a TomTom map and can calculate a route between two locations using the Routing API.

You can find the complete code for the demo used in this article on CodeSandbox.

First, open a terminal on your machine and run the following command, replacing {your_app_name_here} with what you’d want to name the app:

npx create-react-app {your_app_name_here}

Next, within the terminal, navigate into the directory created by CRA and run the command below to install the TomTom Maps Web SDK and the Services SDK. These enable you to display a TomTom map and call the Routing API on your app:

npm install @tomtom-international/web-sdk-maps @tomtom-international/web-sdk-services

Then, open the App.js file within the /src folder of your React app. Clear all the code within it and populate it with the following code:

1import * as React from "react";
2import {useEffect, useRef} from "react";
3
4// CSS styles
5import "./index.css";
6
7// TomTom SDK
8import * as tt from "@tomtom-international/web-sdk-maps";
9import * as tts from "@tomtom-international/web-sdk-services";
10import "@tomtom-international/web-sdk-maps/dist/maps.css";
11
12export default function Home () {
13 const API_KEY = "YOUR_API_KEY";
14 const AMSTERDAM = {lon: 4.896029, lat: 52.371807};
15
16 const map = useRef();
17 const mapContainer = useRef();
18
19 function createRoute () {
20 const routeOptions = {
21 key: API_KEY,
22 locations: [
23 [4.91191, 52.36619],
24 [4.87631, 52.36366],
25 ],
26 travelMode: "truck",
27 vehicleCommercial: true,
28 vehicleHeading: 0,
29 };
30
31 tts.services.calculateRoute(routeOptions).then((response) => {
32 routeOptions?.locations.map((store) =>
33 new tt.Marker().setLngLat(store).addTo(map.current)
34 );
35
36 var geojson = response.toGeoJson();
37 map.current.addLayer({
38 id: "route" + Math.random(100000),
39 type: "line",
40 source: {
41 type: "geojson",
42 data: geojson,
43 },
44 paint: {
45 "line-color": "#0f8ae2",
46 "line-width": 8,
47 },
48 });
49
50 var bounds = new tt.LngLatBounds();
51 geojson.features[0].geometry.coordinates.forEach(function (point) {
52 bounds.extend(tt.LngLat.convert(point)); // creates a bounding area
53 });
54 map.current.fitBounds(bounds, {
55 duration: 300,
56 padding: 50,
57 maxZoom: 14,
58 }); // zooms the map to the searched route
59 });
60 }
61
62 useEffect(() => {
63 map.current = tt.map({
64 key: API_KEY,
65 container: mapContainer.current.id,
66 center: AMSTERDAM,
67 zoom: 10,
68 language: "en-GB",
69 });
70
71 map.current.addControl(new tt.FullscreenControl());
72 map.current.addControl(new tt.NavigationControl());
73
74 return () => {
75 map.current.remove();
76 };
77 //eslint-disable-next-line
78 }, []);
79
80 return (
81 <div className="">
82 <div className="container">
83 <div ref={mapContainer} className="map" id="map" />
84 <button
85 className="btn"
86 onClick={(e) => {
87 e.preventDefault();
88 createRoute();
89 }}
90 >
91 calculate route
92 </button>
93 </div>
94 </div>
95 );
96}

The code above displays a TomTom map on our React application and allows it to call the Routing API.

Remember to replace the “YOUR_API_KEY” placeholder with the API key from your TomTom developer account.

Finally, add some CSS styles to the app.

Open the index.css file within the /src folder, delete all the styles in it, and populate it with the CSS styles below:

1* {
2 padding: 0;
3 margin: 0;
4 box-sizing: border-box;
5}
6.container {
7 width: 100vw;
8 height: 100vh;
9 display: flex;
10 flex-direction: column;
11 justify-content: space-around;
12 align-items: center;
13 position: relative;
14}
15.map {
16 width: 100%;
17 height: 100%;
18}
19.btn {
20 position: fixed;
21 bottom: 4rem;
22 left: 50%;
23 border-radius: 0.5rem;
24 transform: translate(-50%, 0);
25 border: none;
26 padding: 1.2rem 1rem;
27 background: #000;
28 color: #fff;
29 font-size: 1.3rem;
30 cursor: pointer;
31}

Now, when viewed in a web browser, the app should look like the image below:

Photo 1

With our app up and running, let’s see how the routes displayed by the Routing API change based on the value of the vehicleHeading parameter.

In the App.js file, we defined a function — createRoute, which defines the object routeOptions that holds the properties it passes to the Routing API Calculate Route service. This allows the service to determine the possible routes between two locations.

Within the routeOptions object, the vehicleHeading parameter is as follows:

vehicleHeading: 0

The value 0 signifies that the vehicle to route is heading due north to get to its destination.

Clicking calculate route calls the Calculate Route service of the Routing API to construct the route most suited for the vehicle's path.

Here’s the result:

Photo 2

Now, change the value of the vehicleHeading parameter to 90. This signifies that the vehicle is heading due east.

vehicleHeading: 90

Next, click calculate route again. The Routing API takes note of the new value of the vehicleHeading parameter and constructs a route to match its path, different from when the vehicle was heading north.

Here’s the result:

Photo 3

As you keep changing the value of the vehicleHeading parameter (set to 180 in the image below), the Routing API keeps constructing different routes most suited to the vehicle’s path.

Photo 4

Summary

When using the TomTom Routing API, we can define the direction a vehicle is heading to reach a specific destination using the vehicleHeading parameter. The vehicleHeading parameter enables the Routing API to construct the route most appropriate for the path the vehicle is traveling in. By doing this, we eliminate late arrivals and excessive fuel and battery consumption, ensuring that both drivers and customers who use our maps have a positive delivery experience.

Curious about what other features are available in TomTom maps? Sign up for a free developer account and try out TomTom’s wide range of APIs and developer resources, including the TomTom Routing API, today. And as you start integrating TomTom maps into your modern React applications, check out this useful tutorial on how to integrate it seamlessly.

Happy mapping!

Get the developer
newsletter.

No marketing fuff. Tech content only.
Thanks for contacting us

We will reach out to you soon.
Blog cards
tomtom tech news