Tilt and Rotation with TomTom JavaScript Maps SDK for Web v6
Jose Jose Rojas·Sep 17, 2019

Tilt and Rotation with TomTom JavaScript Maps SDK for Web v6

Jose Jose RojasJose Jose Rojas
Jose Jose Rojas
Jose Jose Rojas is Developer Evangelist for the TomTom Developer Relations team. He is passionate about: new product designs, electric scooters and mobile game development. Prior to his time at TomTom, he worked in a variety of roles including software developer, product owner and team lead.
Sep 17, 2019 · 7 min read

TomTom JavaScript Maps SDK for Web 5.0 includes support for new mapping capabilities including the ability to rotate the map and the ability to change the map’s tilt programmatically. There’s even a new flyTo mode that animates the transition between views. In this article, we’ll build an application using the TomTom JavaScript Maps SDK for Web and write code to demonstrate how to set the map location, orientation, and perspective.

Map SDK Updates

The new TomTom JavaScript Maps SDK for Web v6 augments the previous version 5 with more support for new mapping scenarios. It was designed to work on a range of devices and interaction modes, from lower-power small-screen devices to more powerful devices with larger multi-touch screens with scalable vector-based maps. Code demonstrating the new functionality can be found in the functional sample project (available here) and detailed within the SDK documentation.

Let’s look at two of the new map capabilities:

  • The ability to rotate the map.

  • The ability to change the map’s tilt.

The best perspective for a map will depend on what a user is doing.

If a user is planning a trip, the preferred perspective is usually an overhead view with the north direction being in the direction of the top of the display.

When following directions, the preferred orientation is for the direction in which the user is travelling to be in the same direction as the top of the display, rotating as the user makes turns and as the direction of the path being travelled changes. Instead of an overhead view, a tilted perspective is preferred. This is closer to the perspective that a traveler will have when looking at the road.

In this article, we will:

  • Build an application using the TomTom JavaScript Maps SDK for Web.

  • Write code to demonstrate how to set the map location, orientation, and perspective.

Base Project

To get started, make a new HTML project with an index page and folders for the JavaScript and style components. Also, the latest version of the TomTom JavaScript Maps SDK for Web is needed and can be found here. The full documentation on using the SDK and its features can be found there as well. You can also use our content distribution network (CDN) to download the library and css style files, but for this sample we are downloading the files and placing them in a folder named ‘sdk'.

The index page has a script reference to the maps JavaScript file and style definitions.

1<!doctype HTML>
3    <head>
4        <link rel="stylesheet" href="sdk/maps.css" />
5        <style>
6        .map {
7            height: 100%;
8            position: absolute;
9            width: 100%;
10        }
11        </style>>        
12        <script src="sdk/maps-web.min.js"></script>
13        <script src="js/main.js"></script>
14    </head>
15    <body>
16        <div id="mapElement" class="map"></div>
17    </body>

This is the JavaScript to create the map.

3var map;
5function start() { 
6    console.log("running")
7    map = new tt.map({
8        "key": APP_KEY,
9        "container":"mapElement",
10    });
12window.onload = start;

To run the code, it should be accessed through an HTTP server. The easiest way to do this is to use the Node.js utility http-server. The utility can be installed with the Node package manager with the command:

1npm install http-server –g

Once installed, from the command terminal, navigate to the folder that contains the project and type:

1http-server  .

When the server is running, open a web browser and navigate to http://localhost:8080/index.html. You will see the application render a map.

1function start() { 
2    console.log("running")
3    map = new tt.map({
4        "key": APP_KEY,
5        "container":"mapElement",
6        "basePath":"sdk",
7        "source":"vector",
8        // Adding the following two elements to 
9        //set initial map location
10        "center": {lat:37.336634, lng: -121.891623}, 
11        "zoom": 13
12    });

Setting the Map Location

One of the first things that you’ll want to do in the map is change the location on which it is centered. Let’s center the map on San Jose. The coordinates for San Jose are {lat:37.336634, lng: -121.891623} .

When the map is initialized, there are a number of optional values that can be passed to its constructor. A center value (the latitude and longitude) controls the coordinates that will be at the center point of the map.

By default, the map will show the entire earth. Passing a zoom value will enlarge the area being viewed. Larger values will cause the map to be zoomed in more. The following code only differs from the previous code sample in values for center and zoom being set.

1function start() { 
2    console.log("running")
3    map = new tt.map({
4        "key": APP_KEY,
5        "container":"mapElement",
6        "basePath":"sdk",
7        "source":"vector",
8        // Adding the following two elements to 
9        //set initial map location
10        "center": {lat:37.336634, lng: -121.891623}, 
11        "zoom": 13
12    });

When the page is loaded the map now centers on San Jose.


The zoom level and center point can also be set after the map is loaded through the map functions setCenter, setZoom, and flyTo. These functions could be used to set the map location in response to user action.

Both setZoom and setCenter have immediate effect.

flyTo will animate the transition from the maps current position to the selected position. In addition to accepting center and zoom values, flyTo can also accept pitch and bearing values (which will be discussed in a moment).

The following is a modification of the previous code example. After the map loads it will wait for six seconds and then animate in from a world view to San Jose.

1function start() { 
2    console.log("running")
3    map = new tt.map({
4        "key": APP_KEY,
5        "container":"mapElement",//this is the ID of the div element in
6                                 //the HTML which will contain the map
7    });
9    map.on('load',function() {
10        setTimeout( function() { 
11            //Using the flyTo function to smoothly animate 
12            // the map to a new position
13            map.flyTo({
14                "center:{lat:37.336634, lng: -121.891623},
15                "zoom":13
16            })
17        }, 6000)
18    }); 

To instantly jump to the same map position instead of animating to it, use the setCenter and setZoom functions.

1map.on('load',function() {
2    setTimeout( function() {
3        //Instead of using flyTo here I am setting the map
4        //center and zoom to jump to a specific position 
5        map.setCenter([ -121.891623, 37.336634,]);
6        map.setZoom(13);
7    }, 6000)

Using Tilt

The map will automatically start from an overhead view, as though a camera were looking straight down on the area being viewed. This is a perspective with no tilt or pitch (a zero-degree tilt). The camera can be adjusted to have up to a 60-degree tilt.

When combined with the 3D features of the new JavaScript Maps SDK for Web, such as visualization of terrain and buildings, the pitch of the map can be set with a single function call. The map object has a function named setPitch that accepts a number in the range of 0 to 60.

In the following code after the map is loaded the code waits for 6 seconds and then applies a 60-degree tilt.

1function start() { 
2    console.log("running")
3    map = new tt.map({
4        "key": APP_KEY,
5        "container":"mapElement",
6        //Set the initial position of the map to San Jose
7        "center": :{lat:37.336634, lng: -121.891623}, 
8        "zoom": 13
9    });
11    map.on('load',function() {
12        setTimeout( function() { 
13            //Tilting the map perspective 60 degrees
14            map.setPitch(60)
15        }, 6000)
16    });
Code which tilts map six seconds after it has loaded.

The result looks like the following.


Using Rotation

Tilt gives a natural perspective of the map. But by itself the map is always facing the same direction. Maps are usually oriented with the top of the map being aligned with north.

Only being able to face north isn’t useful. When following a path, it is easier for a user to make decisions and follow directions if the map is reoriented with the top of the map to be aligned with the direction in which the user is moving. Instead of the need to mentally translate a change in direction on the map to the actual turn that a user needs to make changes in direction are aligned. The map is rotated by setting the direction (or bearing).

The map is reoriented through the setBearing function. The function accepts the direction that should be oriented in the upward direction in degrees.

  • North is 0 degrees

  • East is 90 degrees

  • South is 180 degrees

  • West is 270 degrees

Another way of thinking of this value is the number of degrees to rotate the map in a clockwise direction.


Apply both rotation and tilt together to give a better presentation of the map. With both effects applied the map can have a perspective that is close to the first-person perspective that the user will have. This could be done by calling setPitch and setBearing individually, or in a single call using flyTo.

If the flyTo function is used instead of the map popping to the desired state it will smoothly rotate and tilt to transition to the new state. In addition to being more visually pleasing, a user is able to better understand the relationship between a previous and updated perspective of the map through seeing the transition.

1map.on('load',function() {
2    setTimeout( function() { 
3        //using flyTo again, but with a different set of values. 
4        //Previously the center and zoom were being set. Now the
5        //pitch and bearing are being set. 
6        map.flyTo (
7            {
8                "pitch":60,
9                "bearing":65,
10                "center": {lat:37.336634, lng: -121.891623},
12            }
13        )
14    }, 6000)
Use flyTo(newposition)to have the map glide into position.SDK05


Within this article we’ve seen how to make display a vector map using the TomTom JavaScript Maps SDK for Web v6. You have seen how to programmatically change the perspective of the map by setting the maps rotation and tilt, allowing the map to be changed to an overhead perspective for planning to a perspective closer to first person which is better for following directions and making navigation decisions on the go. These changes of perspective can be dynamically adjusted and in response to user interaction.

Get the developer newsletter.
No marketing fuff. Tech content only.

* Required field. By submitting your contact details to TomTom, you agree that we can contact you about marketing offers, newsletters, or to invite you to webinars and events. We could further personalize the content that you receive via cookies. You can unsubscribe at any time by the link included in our emails. Review our privacy policy.