Adding TomTom Maps to a Vue + Quasar App

We’ll create a sample Quasar application and create a route with restrictions for trucks using TomTom’s truck routing API.

TomTom Developers
TomTom Developers
Published in
7 min readJul 1, 2021

--

Maps enhance web applications across all industries, telling stories and answering questions, all while helping customers find your business. Quasar applications are no exception. With TomTom, you can add advanced location technology to your Quasar app in just a few minutes. TomTom’s easy-to-use maps enhance user experience.

In this tutorial, we create a sample Quasar application. Its JSON file contains example store location information we render on a TomTom map using markers. We then create a route with restrictions for trucks using TomTom’s truck routing API. To follow this tutorial, you should be familiar with Quasar, or at least Vue, and JavaScript.

INSTALLING QUASAR

To get started, we must install the Quasar command-line interface (CLI) using:

Ensure you have Node.js version 10 or higher installed on your machine. Once the installation is done, create a project folder with the Quasar command:

This command prompts you to enter a project name, project product name, project description, and author details. Most developers should find the installation steps easy to follow. When all dependencies are installed, a default web browser tab opens automatically:

Our default app already has some data. Change the header to “Essential Delivery” in the main layout file and erase the data accordingly.

SETTING UP TOMTOM

We next set up TomTom to enhance our location-based application using Map APIs and SDKs. First, register your free account on TomTom Developer Portal. Then, install the TomTom Web SDK with:

To use TomTom Maps SDK and other services, you will need to first get your API key from TomTom Developer Portal. In the sample codes that follow, be sure to replace “YOUR_API_KEY” with your non-expiring TomTom API key.

You can also explore documentation within the Developer Portal to learn how to use TomTom’s various tools to create other great applications.

DISPLAYING THE TOMTOM MAP

Before we add a TomTom map to our application, we must add the maps.css file to our application page header. Without this, it would be challenging to display certain parts of our map and objects that interact with it. So, in index.template.html, add this snippet:

We need a component to hold our maps and related items. Let’s start by editing the EssentialLink.vue file in src > components. This file helps us display stores in our application’s left drawer. Paste this code in the EssentialLink.vue file:

This file’s properties prop helps us display store names and addresses on our page. The list in this component gets its data from a JSON file. We don’t have that yet, so let’s add it. In src, create a file called “stores.json” and paste in this data:

This JSON file stores address and coordinate data that helps us display markers and calculate routes.

Next, we create a file called “Search.vue” inside the components folder. This file helps us display and zoom in on a city using the TomTom Search service. Paste in this code:

Now, let’s create just one more component, called “TomTomMap.vue”. This holds our map as well as search and link components. Paste in this code:

The top of this code contains the EssentialLink and Search components. In the script section, we import our components, services, and JSON file containing store information.

The handleSearchResult method pans our map to the location of choice, then calls the init method. The init method initializes our map and creates markers for items in our JSON file. Markers are easier to see if they are in the city you specify.

In mounted, we initialize our map object and build an object called shopLoc. This object stores the name and coordinates of each shop and makes them easier to manipulate and reference.

Now, all this only works if we place our TomTomMap component on a page. So, in pages, open Index.vue and paste in this code:

We place our API Key here and specify a city to zoom in on. In this example, the city is Amsterdam.

We can now start using TomTom Routing API. To import the Maps SDK for Web, place this line at the top of the <script> section of your TomTomMap.vue file:

Next, place two drop downs to help users select the start and stop location, and buttons to clear a route and make a route. Additionally, add a toggle switch to tell TomTom API if we are carrying hazardous goods. If true, the API returns the safest route for that cargo. It’s an interesting (and helpful) feature.

To do this, paste this code right below the <EssentialLink /> component in TomTomMap.vue:

Now, for the logic behind it all, paste this code in the methods section of your TomTomMap.vue file:

This code has three functions. One function’s details help draw a route on your map called buildStyle. The function makeRoute finds and builds out a route on your map. It does this by calling TomTom Routing API to find the best route. This parses your response data, too, and uses the addLayer to map a route on the page. Lastly, clearRoute clears your route in case you want to change start and stop locations.

Your app should now look pretty cool. Remember the toggle switch we mentioned? Try it out! TomTom API provides optimized routes by passing certain parameters about your cargo type. It suggests alternative routes that comply with local rules and regulations for transportation of goods by vehicles, such as hazardous waste.

This image displays a route from Valkensteeg 3, 1012 MH to Waterlooplein 29A, 1011 NX:

Notice how the route changes when we toggle Hazardous Waste.

Our app is now complete. This app’s foundation comes from this GitHub repo.

NEXT STEPS

We have set up everything needed to create a basic Quasar app, added a TomTom map from a JSON file, and showed routing for restricted and unrestricted routes. Easy, right?

Now that you know how to integrate TomTom maps into Quasar, you can create and design applications for different regions, different business types, different route options, and more. Sign up for a developer account and get your free API key to get building today.

Happy mapping!

This article originally appeared on https://developer.tomtom.com/blog.

--

--

TomTom Developers
TomTom Developers

We love maps. We love data. We love developers. We’re here to help developers build the next generation of location-based applications.