Maps SDK with TypeScript
Create a map using the SDK based on TypeScript. Benefit from features like code completion, type safety, accurate documentation, and backward compatibility. Save time on your project by reducing typos and mistakes. No need to worry about changes to API endpoints and how they may affect your code.
Choose from a wide range of bundlers to find the best solution for your project. Whether you prefer Parcel, Webpack, Rollup, Turbopack, or any other bundler, you have plenty of options to choose from.
-
Install the npm package.
npm install --save @maptiler/sdk -
Create a
<div>element with a certain id where you want your map to be.Add
<div>tag into your page. This div will be the container where the map will be loaded.<div id="map"></div> -
Include the following code in your TypeScript
main.tsfile.import { config, Map, MapOptions } from '@maptiler/sdk'; import "@maptiler/sdk/dist/maptiler-sdk.css"; config.apiKey = 'YOUR_MAPTILER_API_KEY_HERE'; const options: MapOptions = { container: document.getElementById("map") as HTMLElement, // container's id or the HTML element in which SDK will render the map } // Instanciating a map const map: Map = new Map(options); -
Replace
YOUR_MAPTILER_API_KEY_HEREwith your own API key. Make sure to secure the key before you publish it. -
Create the map containter style. Add the map containter style to you stylesheet
style.css. The div must have non-zero height. In this case we are going to make a fullscreen map.body { margin: 0; padding: 0; } #map { position: absolute; top: 0; bottom: 0; width: 100%; }
Check out the tutorial How to create a map with TypeScript and Parcel.
-
Install bundler and build tools. This tutorial will use Parcel because it supports TypeScript out of the box without any additional configuration.
npm i -D parcel-bundler typescript -
Create the TypeScript config file.
npx tsc --init -
Update the package.json scripts. Create the
devandbuildscripts."scripts": { "dev": "parcel src/index.html", "build": "parcel build src/index.html" }, -
Create a
srcfolder and inside this forder create 3 files:index.html,main.ts, andstyle.css. -
Copy the following code, paste it into
index.htmlfile. The<div id="map">will be the container where the map will be loaded.<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>MapTiler SDK JS with TS</title> <link rel="stylesheet" href="./style.css"> <script src="./main.ts" defer></script> </head> <body> <div id="map"></div> </body> </html> -
Create the map container style. Add the map container style to you stylesheet
style.css. The div must have non-zero height. In this case we are going to make a fullscreen map.body { margin: 0; padding: 0; } #map { position: absolute; top: 0; bottom: 0; width: 100%; } -
Include the following code in your
main.tsfile.import { config, Map, MapOptions } from '@maptiler/sdk'; import "@maptiler/sdk/dist/maptiler-sdk.css"; config.apiKey = 'YOUR_MAPTILER_API_KEY_HERE'; const options: MapOptions = { container: document.getElementById("map") as HTMLElement, // container's id or the HTML element in which SDK will render the map } // Instanciating a map const map: Map = new Map(options); -
Replace
YOUR_MAPTILER_API_KEY_HEREwith your own API key. Make sure to secure the key before you publish it. -
Running the dev server. To see the map run the local server. Open a terminal and write.
npm run dev -
Now you can see your application opening
http://localhost:1234in your browser. By default, the map will be initialized with the style maptilersdk.MapStyle.STREETS and centered on the actual visitor location using the IP geolocation API provided by MapTiler.