Build Different

Displaying Multiple Map Locations with Vue and the TomTom Web SDK

Kingsley Ubah
Jan 19, 2022 • Last edit on Sep 20, 20228 min read

Countless online platforms base their primary services upon reliable geolocation and mapping. Whether we want to include mapping tools on a website or build the next Uber-style mobile app, TomTom Maps software development kit (SDK) enables us to incorporate mapping and location-based features into our web, Android, or iOS application. This SDK exposes several APIs that grant access to various features, ranging from maps with just the bare essentials to route-planning capabilities and fuzzy search functions.

The TomTom Maps SDK integrates capably with increasingly-popular front-end frameworks like React and Vue. It conveniently hides bare RESTful calls from developers, further simplifying its use.

This article guides you through building a fast, dynamic, and performant location-based Vue application using the TomTom Map SDK. Our application will enable users to specify multiple map locations to display as markers on a dynamic map, helping them keep track of relevant places. You should be familiar with JavaScript to follow along.

Exploring Vue 3 and Vite

Vue is one of the most popular JavaScript libraries for building user interfaces. Vue 3 is the newest version of Vue.js, introducing features that make end applications lighter, faster, and leaner.

One such feature is the new Composition API. This set of additive functions enables a more understandable code organization system in Vue apps.

The Vue.js team also introduced a tool for their teeming community of developers to enhance development speed and efficiency. Vite is an advanced front-end building solution for web applications. It even features a dev server with impressively-fast hot module replacement. Vite is framework agnostic, allowing you to use it with various alternatives.

In the following sections, we will:

• Set up Vite to create a new Vue 3 project using the composition API • Integrate the TomTom Map SDK into a Vue app from the content delivery network (CDN) • Create a Map component that renders a TomTom map with multiple marked locations when first mounted to the document object module (DOM) • Manage map coordinates data using Vue’s reactive state system • Create a UI Form component for collecting new coordinate values that we will add to the map without repainting the map on the browser

Visit GitHub for the source code and follow the installation guide below for complete setup instructions.

Setting up the Project

Let's start by initializing a fresh Vite project using the command-line interface (CLI). Open your command terminal, cd into the project’s target folder, and run the following command:

npm init @vitejs/app

The CLI asks for information about your application, starting with the project name. We use vue-tomtom.

Be sure to select Vue as the development framework. When setup is complete, run the following commands:

cd vue-tomtom
npm install

This command quickly installs all other project dependencies from npm.

When the installation is complete, open your project inside your code editor. You should find a folder structure like this:


Finally, run the Vite dev server with the following command:

npm run dev

This command runs your dev server at https://localhost:3000, which should display the Vite starter template:


Now let’s check out the TomTom SDK.

Using TomTom’s Map SDK in Vue

Before bringing the TomTom Map SDK into our project, we must register an account to receive an API key for our application. Read how to get a TomTom API key for more information. It’s free to register an account if you don’t already have one, and you get thousands of free requests per day. No credit card is needed to sign up and you only pay as your application grows more popular.

You can use the TomTom Maps SDK through various channels. To learn more, visit the Map SDK’s downloads section. This guide uses the CDN version 6.

Now, in your code editor, go to vue-tomtom/index.html and include links to the script and style files from the CDN as follows:

1<!DOCTYPE html>
2<html lang="en">
3 <head>
4 <!-- [...] -->
5 <link rel='stylesheet' type='text/css' href=''>
6 </head>
7 <body>
8 <div id="app"></div>
9 <script type="module" src="/src/main.js"></script>
10 <script src=""></script>
11 <script src=""></script>
12 </body>

Now we can use the Maps and Services API to create a TomTom map.

Displaying a Map

Let's create a Map component to display a map.

Inside your project’s src/components folder, create a Map.vue file with the following code:

2 <div>
3 <!-- <Form /> -->
4 <div ref="mapRef" id="map"></div>
5 </div>
10export default {
11name: 'Map',
12// [...]

The Map component renders a Form component (which we implement later) and a wrapper div element to insert the map. We later reference this wrapper by its id and ref attributes.

Next, in the script section of Map.vue, we include some native imports and additional code:

2import {onMounted, reactive, ref } from 'vue'
4export default {
5 name: 'Map',
6 setup() {
7 const mapRef = ref(null)
9 onMounted(() => {
10 const tt =;
11 const focus = { lat: 6.4434, lng: 3.3553 }
13 var map ={
14 key: 'Ka5JcBchkVsJSr3gxVgHPC1NcvPO90lG',
15 container: mapRef.value,
16 center: focus,
17 zoom: 15
18 })
20 map.addControl(new tt.FullscreenControl());
21 map.addControl(new tt.NavigationControl());
23 = map
25 // insertLocs(map)
26 })
28 return {
29 mapRef
30 }
31 },
33// [...]

Using the Vue Composition API’s ref function in this code, we created a ref variable, which provides a reference to the DOM element where we will insert our map. Then, inside the onMounted hook, which runs after mounting a component on the DOM, we initialized a new map, passing in an options object containing the following properties:

• Key is our application’s API key, from the developer dashboard • Container is a reference to the div where we want to insert the map • Center is our map’s focus area • Zoom is our map’s zoom level

We also passed our map instance to a global object for future reference.

Finally, we ran the insertLocs function to fill the map with multiple markers. We commented out the function because we had not yet defined it. So, let’s define the function next.

Adding Multiple Locations to the Map

In the src/components/Map.vue file, add the following code inside the setup function:

1setup() {
2 const state = reactive({
3 locations: [
4 { lat: 6.4434, lng: 3.3553 },
5 { lat: 6.4442, lng: 3.3561 },
6 { lat: 6.4451, lng: 3.3573 },
7 { lat: 6.4459, lng: 3.3520 }
8 ]
9 })
11 const insertLocs = (map) => {
12 const tomtom =;
14 state.locations.forEach(function (location) {
15 var marker = new tomtom.Marker().setLngLat(location).addTo(map)
16 const popup = new tt.Popup({ anchor: 'top' }).setText('UBABank')
17 marker.setPopup(popup).togglePopup()
18 })
19 }
20 // [...]
21 },

The reactive function is part of the Vue reactivity system and manages reactive state data in a Vue application. We created a state object using the reactive function, then passed an array of coordinates to the locations property.

When we invoke insertLocs, the function recursively inserts four new markers inside our map. These markers appear immediately after the app mounts the Map component.

The following section examines how to collect and dynamically insert a user-added location.

Adding a New Location

Let's create and render a form to collect location coordinates from the user interface (UI).

Inside the src/components folder, create a Form component by making a Form.vue file with the following code:

2 <div class="form">
3 <input type="text" placeholder="longitude" v-model="longitude">
4 <input type="text" placeholder="latitude" v-model="latitude">
5 <button @click="sendLocation">Add Location</button>
6 </div>
10import { ref } from 'vue'
12export default {
13 setup(props, context) {
14 const latitude = ref('')
15 const longitude = ref('')
17 const sendLocation = () => {
18 context.emit('useLocation', {lat: Number(latitude.value), lng: Number(longitude.value)})
19 }
21 return {
22 longitude,
23 latitude,
24 sendLocation
25 }
26 },

This code defined two inputs with the v-model directive. This approach binds the inputs to their respective ref variables, enabling us to access both latitude and longitude values from the UI.

When the user clicks Submit, we emit an event (useLocation) and an object containing both input values to the parent component, Map.

Now, inside Map.vue, handle the event as follows:

2 <div>
3 <Form @useLocation="useLocation" />
4 <div ref="mapRef" class="map"></div>
5 </div>
9import Form from './Form.vue'
11export default {
12 components: {
13 Form
14 },
15 setup() {
16 // [...]
17 const useLocation = (location) => {
18 state.locations.push(location)
19 const lastLocation = state.locations[state.locations.length - 1]
21 const tete =;
22 new tete.Marker().setLngLat(lastLocation).addTo(
23 }
24 // [...]
25 }

We imported and registered the Form component as a child of Map in this code. Then we listened for the custom useLocation event, for which we called the useLocation function.

We first updated our component state with the emitted location inside the function. Then we grabbed the latest location data from the array and inserted that location into our Map instance (stored in a Windows object).

As a result, clicking the button emits an event from child to parent, which updates the reactive state data of Map and adds the new marker to the map.

Using the Map

Now that we have implemented the Map and Form components, it's time to use them in App.vue.

First, open the src/App.vue file and replace the boilerplate code with the following:

2 <Map />
5<script setup>
6import Map from './components/Map.vue'

Save the file and go to https://localhost:3000. Now try adding new locations through the form.

Whenever we add a new location, a flag appears on the map to mark that location. The map continues displaying all places as we add them.

Next Steps

Throughout this article, we have experienced the ease of integrating the TomTom Web SDK into a modern Vue 3 app, enabling the use of real-time location-based features.

The Map Display API is only one of TomTom Web SDK’s many seamless integrative services. You can learn more about TomTom Web SDK features from the TomTom API documentation.

Sign up for a free TomTom Developer account now to introduce top-tier map features into your application.

Get the developer

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

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