Build Different

Using the TomTom Maps Web SDK in a NativeScript Web View App

Developer Portal
Oct 23, 2021 • Last edit on Sep 20, 20229 min read
Two laptops showing a map and NativeScript platform

Smooth map browsing experiences are essential in today’s increasingly digital world. Convenience aside, online maps provide opportunities for users to find hidden gems they may have otherwise missed. So, it’s no surprise companies are racing to offer better map browsing capabilities.

There are a handful of ways to integrate maps into your site for a smoother user experience. Today we’re going to take a look at one such solution: TomTom Maps.

We here at TomTom built our Maps with innovative technology, enabling users to know the road ahead, stay on track with the freshest maps, stay ahead of changing regulations, and keep the world at their fingertips.

TomTom Maps is available in NPM and CDN. And now, developers can integrate TomTom Maps into mobile applications, especially in a NativeScript set up.

But, using TomTom Maps in a NativeScript application can pose some problems. Wrapping the map in a native component for every platform you deploy is tedious and time-consuming. Also, the maps aren’t able to maintain a consistent display on different platforms. This will lead to all sorts of difficulties when it comes to designing, testing, and debugging.

This article teaches you how to confidently use NativeScript in a WebView to solve the above pain points and how to ensure the smooth operation of TomTom Maps in different operating systems.


To build your app, you need to have some knowledge of JavaScript, TypeScript, and how NativeScript works.

Additionally, ensure you have the following tools installed:

  • Node.js: NativeScript’s tooling depends on Node.js, so the Node.js binary must be available.
  • ns command: This is a NativeScript CLI tool we use to scaffold a NativeScript application, run and build NativeScript applications, and more. To install it, run the command: npm i NativeScript -g.
  • Java: NativeScript uses the Java Runtime for compiling the NativeScript applications to an Android mobile application. Right now, NativeScript supports JDK 15, so download the Java Runtime for your machine. Using Mac OS, you can install JDK 15 by running the command: brew tap AdoptOpenJDK/openjdk & brew install --cask adoptopenjdk15.
  • Android SDK: We use this for NativeScript when compiling and building an application for an Android file.
  • Android Emulator: This is a virtual Android phone that runs on our system. It comes bundled with the Android SDK, so make sure yours is running when building your application.

NativeScript Project Setup

Let’s look at how to set up a NativeScript application. Ensure your NativeScript ns CLI is available in your system by running the command ns --version.

Now, create the application:

ns create tomtomprj

When prompted to select a template, choose React.js and press Enter. This creates a NativeScript project in the tomtomprj folder. NativeScript lets you create native mobile apps using React, Angular, and Vue.

We’ve chosen React because it’s widely known and trusted among JavaScript developers. Additionally, using React will also easily let us compare this app and the React Native equivalent we created in this earlier article.

Next, move into the folder by running cd tomtomprj. NativeScript’s built-in WebView component doesn’t support bidirectional communication between the WebView and our app’s native code. Fortunately, the nativescript-webview-ext plugin contains an enhanced WebView that provides everything we need. Install it by running:

npm install @nota/nativescript-webview-ext

Let’s try tunning the sample application. If you’re using an Android device or emulator, run:

ns run android

Or if you are running an iOS device or emulator, run:

ns run ios

The result will look like this:


As shown above, NativeScript created our application with some template code. If you’d like to take a look at our app’s complete and final code before you proceed, it is available on GitHub.

How To Use TomTom Maps with NativeScript WebView

First, you need to get your TomTom API key. To do that, register in the TomTom Developer Portal. After, go to your email to activate the account. Then, go to our Developers TomTom dashboard and copy your API key.

The best practice for embedding a TomTom Map in NativeScript is to scaffold the NativeScript app, render the WebView component, and pass an HTML string to the WebView component’s src attribute.

Before we do any of that, however, we must register the nativesceipt-webview-ext plugin so we can use it in our app. We can do this by adding the following code to src/app.ts:

1import { registerElement, NativeScriptProps } from "react-nativescript";
2import type { WebViewExt } from "@nota/nativescript-webview-ext";
4registerElement('webViewExt', () => require("@nota/nativescript-webview-ext").WebViewExt);
6declare global {
7 module JSX {
8 interface IntrinsicElements {
9 webViewExt: NativeScriptProps<any, WebViewExt>,
10 }
11 }

Now, open your src/components/HomeScreen.tsx file. We chose HomeScreen because it’s the first screen displayed when our app launches.

First, clear the original code up to the flexboxLayout element:

1import * as React from "react";
2import { RouteProp } from "@react-navigation/core";
3import { Dialogs } from "@NativeScript/core";
4import { FrameNavigationProp } from "react-NativeScript-navigation";
5import { StyleSheet } from "react-NativeScript";
6import { MainStackParamList } from "./NavigationParamList";
9type HomeScreenProps = {
10 route: RouteProp<MainStackParamList, "Home">;
11 navigation: FrameNavigationProp<MainStackParamList, "Home">;
15export function HomeScreen({ navigation }: HomeScreenProps) {
16 return <flexboxLayout style={styles.container}></flexboxLayout>;

Next, we set up a variable to hold our HTML string. The HTML string renders a TomTom map in a div element.

1const htmlString = `
3 <head>
4 <link
5 rel="stylesheet"
6 type="text/css"
7 href=""
8 />
9 <script src=""></script>
10 </head>
11 <body style="width: 100%; height: 100%; margin: 0; padding: 0">
12 <div
13 id="map"
14 style="width: 100%; height: 100%; background-color: orangered"
15 ></div>
16 <script>
18 var map ={
19 key: 'YOUR_API_KEY',
20 container: 'map',
21 style: 'tomtom://vector/1/basic-main',
22 center: [-121.913, 37.361],
23 zoom: 15
24 });
26 window.addEventListener("ns-bridge-ready", function(e) {
27 var nsWebViewBridge = e.detail || window.nsWebViewBridge;
28 map.on('dragend', function() {
29 let center = map.getCenter();
30 nsWebViewBridge.emit("mapCenterChanged",
31 center.lng.toFixed(3) + ", " +;
32 });
33 });
35 function setCenter(lng, lat) {
36 map.setCenter([lng, lat]);
37 } </script>
38 </body>

This is basic HTML that renders TomTom Maps in a div element. First, we imported the TomTom Maps stylesheet and the Maps SDK JavaScript file. Next, we set up a div element with an id. We have a script section where we initialized the map and rendered it in the RouteProp element. We call the map function on the tt instance inside the script section, passing an object to it. This object has these properties:

  • The key property is the API key for our TomTom developer account.
  • The container element is the HTML element where the map renders.
  • The style property sets our map’s style. Here, we’re using a default style, but you can customize your map’s look and feel with the TomTom Map Styler.
  • The center property sets the initial longitude and latitude on which the map is centered.
  • The zoom property sets the map’s zoom level.

Then, we add an event listener that sends the map’s new center coordinates back to the native part of our app when a user taps and drags the map view.

Finally, we add a setCenter function we can call into from our native code to change the map’s center position.

Now, we render WebView in our JSX and make its src attribute point to our htmlString variable.

Next, let’s look at the code in HomeScreen.tsx that generates our app’s user interface:

1// ...code truncated
2export function HomeScreen({ navigation }: HomeScreenProps) {
3 let webView = undefined;
4 let [mapCenter, setMapCenter] = useState('-121.913, 37.361');
6 function onButtonPress() {
7 const [lng, lat] = mapCenter.split(",");
8 webView.executeJavaScript(`setCenter(${parseFloat(lng)}, ${parseFloat(lat)});`);
9 }
11 const handleMapEvent = (args: WebViewEventData) => {
12 setMapCenter(;
13 }
15 function initializeWebView(viewComponent) {
16 webView = viewComponent;
17 webView.on("mapCenterChanged", handleMapEvent);
18 }
20 return (
21 <stackLayout style={styles.container}>
22 <label style={styles.headingText}>TomTom NativeScript WebView</label>
23 <flexboxLayout>
24 <textField style={styles.textInput}
25 text={mapCenter}
26 onTextChange={(args) =>setMapCenter(args.value)}/>
27 <button onTap={onButtonPress} text="Set Center" />
28 </flexboxLayout>
29 <webViewExt
30 ref={(r) => { r && initializeWebView(r._nativeView);}}
31 src={htmlString}
32 style={styles.webView} />
33 </stackLayout>
34 );

There’s a lot going on here, so let’s go through it piece by piece.

We start by adding a webView variable to hold a ref to our WebView, and create a state hook to keep track of our map’s center coordinates.

Next, we add a buttonPressHandler function to change the map’s center coordinates in the WebView based on the coordinates entered in our Nativescript text editor component. Note that we’re calling executeJavascript here. This function is important because it lets our Nativescript code execute JavaScript inside the WebView.

Then, we add a handleMapEvent function to handle map coordinate updates passed back into Nativescript from the WebView. As we saw above, the JavaScript in our WebView emits an event whenever the user scrolls the map. We receive that event in handleMapEvent and use the event’s data to update the coordinates displayed in our text field.

We also do a bit of map initialization housekeeping in initializeWebView.

Finally, we return the JSX that actually renders our app. It’s made up of standard React Natievscript components. Note that we hook our button up to the handler function described earlier, set our text field to automatically update our center coordinates if the user enters new values in the textbox, and pass our WebView’s ref into initializeWebView.

Now, run the app again using ns run android or ns run ios. Our app starts up on our device or emulator, and our application appears:


And that’s it! If you drag the map around, you’ll notice the coordinates in the text field update in real time as the map embedded in the WebView passes data back to Nativescript. You can also edit the coordinates in the box manually and tap the ‘Set Center’ button to see that the exchange works in both directions because Nativescript successfully runs code inside the WebView.

You can view the final source code for this project here.


As demonstrated, it’s straightforward to build a platform-agnostic map application using NativeScript WebView and TomTom Maps SDK.

To add TomTom Maps to a NativeScript WebView is very simple. The best practices are to scaffold your project, render the WebView element, set your HTML string in a variable, and then pass the HTML string to the src attribute in the WebView component.

To recap further, we installed the NativeScript CLI tool. Then we scaffolded a NativeScript app. We created an HTML string that contains the HTML that renders TomTom Maps. Next, we rendered a WebView component in our JSX and pointed the HTML string to the src attribute of the WebView component. We then rendered a TomTom Map in the WebView component.

All in all, TomTom Maps is an excellent tool for navigation purposes. Furthermore, this solution also offers:

  • Extended Search API
  • Routing API
  • Extended Routing API
  • Traffic API
  • Geofencing API
  • Location History API
  • Notifications API
  • Search API

To learn more about TomTom Maps and start using it in your applications, sign up for a TomTom Developer account today!

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