Sorry, you need to enable JavaScript to visit this website.

Migrate from version 5 - the Services library

Overview of the tutorial

This tutorial covers basic use cases to help you switch our services. For example, switching the Routing API or Search API services from version 5 of the Maps SDK for Web to our new version. We will use code examples to explain the main differences between the SDKs.

You can check how to migrate map displaying here: Migrate from version 5 - the Maps library

To start using the latest version of the TomTom Maps SDK for Web you need the following:

  • The Services library
    The Maps SDK for Web can be used in three ways:
    • Downloaded from NPM: https://www.npmjs.com/package/@tomtom-international/web-sdk-services
    • With a CDN: https://api.tomtom.com/maps-sdk-for-web/cdn/6.x/<version>/services/services-web.min.js - NOTE: include the latest version in the URL.
    • Downloaded from here: Downloads.

    In this tutorial, we are using a CDN as an example.

  • API Key - If you don't have an API Key visit the How to get a TomTom API Key site and create one.
  • Add the following to the <head> element of your HTML document:

    <!-- Replace version in the URL with desired library version -->
    <script src='https://api.tomtom.com/maps-sdk-for-web/cdn/6.x/<version>/services/services-web.min.js'></script>

    Maps and services use the same namespace and will automatically attach themselves to the tt object. Services will be accessible using tt.services.

Displaying a route/directions

Here we have the code to display a route from point A to point B on the map.

Version 5 of the Maps SDK for Web

tt.services.calculateRoute({
    key: '<your-api-key>',
    locations: '-122.510601,37.768014:-122.478468,37.769167'
})
.go()
.then(function(response) {
    var geojson = response.toGeoJson();
    map.addLayer({
        'id': 'route',
        'type': 'line',
        'source': {
            'type': 'geojson',
            'data': geojson
        },
        'paint': {
            'line-color': '#00d7ff',
            'line-width': 8
        }
    });
    var bounds = new tt.LngLatBounds();
    geojson.features[0].geometry.coordinates.forEach(function(point) {
        bounds.extend(tt.LngLat.convert(point));
    });
    map.fitBounds(bounds, {padding: 20});
});

Latest version of the Maps SDK for Web

tt.services.calculateRoute({
    key: '<your-api-key>',
    locations: '-122.510601,37.768014:-122.478468,37.769167'
})
.then(function(response) {
    var geojson = response.toGeoJson();
    map.addLayer({
        'id': 'route',
        'type': 'line',
        'source': {
            'type': 'geojson',
            'data': geojson
        },
        'paint': {
            'line-color': '#00d7ff',
            'line-width': 8
        }
    });
    var bounds = new tt.LngLatBounds();
    geojson.features[0].geometry.coordinates.forEach(function(point) {
        bounds.extend(tt.LngLat.convert(point));
    });
    map.fitBounds(bounds, {padding: 20});
});

Differences in APIs

Here is a summary of the most important updates in the services library: