## MapTiler Client - v1.3.0

offcial page →

The Javascript & TypeScript API client library to enjoy MapTiler Cloud's
services such as geocoding, geolocation and more!

# API Client Library for JavaScript / TypeScript

## What?

The MapTiler Client JS exposes a number of handy functions that wrap API call to MapTiler Cloud API services, such as:

- Geocoding forward and reverse
- Geolocation from visitor's IP address
- Coordidinate systems search and tranform
- User data fetching as GeoJSON
- Static maps of all sorts

## Why?

The project is entirely written in TypeScript and all the function arguments are nicely documented and typed.

- Better developer experience of working with APIs in the code editor - with parameter info, quick info, and member lists.
  - Code completion: reducing typos and other common mistakes
  - Content assist and code hinting providing contextual help
- Type safety - for higher quality code and testing
- Backward compatibility: guaranteed on changes to API endpoints
- Runs: in Node.js or in browser
- Open source license

# Install

    npm install --save @maptiler/client

# API documentation

In addition to the details and examples provided in this readme, check out the complete API documentation.

# Quick start

    // Import the whole library
    import * as maptilerClient from '@maptiler/client';

    // Or import only the bits you need
    import {
      config,
      geocoding,
      geolocation,
      coordinates,
      data,
      staticMaps,
    } from '@maptiler/client';

The examples folder includes are featuring usages for NodeJS, browser with UMD and browser with ES module.

# Easy access to MapTiler Cloud API

Here is the list of service wrapper functions that are built-in:

## 🔍 Geocoding

> ✅ Please, use geocoding functions only from client-side (browser) and do not 🚫 store or redistribute MapTiler Cloud API data. In case of doubt, consult the terms ⚖️

### Forward

You want to know the longitude and latitude of a specific place, use the forward geocoding:

    // in an async function, or as a 'thenable':
    const result = await maptilerClient.geocoding.forward('paris');

You can provide some options such as:

- the proximity, given a lon-lat position, to sort the results
- one of more languages to get the results into
- a bounding geo box, to restrict the search to a given window

Read more about forward geocoding on our official documentation.

### Reverse

You wan to tknow the name of a place, given a longitude-latitude? Use the reverse geocoding:

    // in an async function, or as a 'thenable':
    const result = await maptilerClient.geocoding.reverse([6.249638, 46.402056]);

The same option object as the forward geocoding can be provided.

Read more about reverse geocoding on our official documentation.

### Language

For both forward and reverse geocoding, this library provides a list of supported languages as shorthands to ISO language codes. The result will be provided in multiple languages if the `language` options is an array:

    const result = await maptilerClient.geocoding.forward('paris', {language: [maptilerClient.geocoding.languages.SPANISH, maptilerClient.geocoding.languages.KOREAN]})

The special language `AUTO` will detect the platform/browser preferred language.

## 🕵️‍♂️ Geolocation

The geolocation service provides location informations of a visitor using its IP address.

The geolocation uses the IP address of a visitors to provide informations about their location, such as city, region, country, timezone, etc. The precision is lower than GPS but does not require visitors to explicitely enable the location service from their web browser.

There is only a single function:

    // in an async function, or as a 'thenable':
    const result = await maptilerClient.geolocation.info();

Read more about geolocation on our official documentation.

## 🌐 Coordinates

If you are already familiar with epsg.io (created by MapTiler), then you may find convenient to access the details of more than 10 thousands of coordinate reference systems (CRS) programmatically, as well as transforming coordinates from one system to another!

### Search

The `search` lets you perform a query in a free form fashion. Here are some examples:

    // in an async function, or as a 'thenable':
    const resultA = await maptilerClient.coordinates.search('mercator');
    const resultB = await maptilerClient.coordinates.search('plate carree');
    const resultC = await maptilerClient.coordinates.search('france');
    const resultD = await maptilerClient.coordinates.search('code:4326', {transformations: true}));

The `transformations` options retrieves a lot more details about the CRS that MapTiler API is able to transform to/from than just their IDs.

Read more about searching coordinate systems on our official documentation.

### Transform

Transforming a couple of coordinates from one system to another can be challenging, for example, most countries have their own official system, yet web mapping tools are more often than not exclusive to WGS84.

If not provided, both the source (`sourceCrs`) and the destination (`targetCrs`) are default to EPSG:4326 (in other words, WGS84). Here is how to use this feature:

    // in an async function, or as a 'thenable':

    // Providing one coordinate to transform, with a target CRS being EPSG:9793 (RGF93 v2 / Lambert-93, France official CRS)
    const resultA = await maptilerClient.coordinates.transform([1, 45], {targetCrs: 9793})

    // Using the same logic, we can pass up to 50 coordinates to be transformed
    const resultB = await maptilerClient.coordinates.transform([[10, 48], [1, 45]], {targetCrs: 9793})

Read more about transforming coordinates on our official documentation.

## 💽 Data

MapTiler Cloud give its users the possibility to upload and create data, manually with a user interface or by uploading a GPX, GeoJSON, KML or shp file. A unique ID is associated to each dataset so that we can later on access it programmatically to retrieve a GeoJSON equivalent of it:

    // in an async function, or as a 'thenable':
    const result = await maptilerClient.data.get('my-dataset-unique-id')

Since the result is a GeoJSON, it can easily be added to a `map` with `.addSource()` and `.addLayer()`.

Read more about fetching your own data on our official documentation.

## 🗺️ Static maps

> ✅ Please, use static maps URLs only from client side `<img>` elements, and do not 🚫 store or redistribute the static map files. In case of doubt, consult the terms ⚖️

Maptiler Cloud provides many possibilities for creating static maps as PNG, JPEG or WebP images. They all offer the possibilities to:

- Choose from one of the MapTiler style or your own
- Add markers with a custom icon (or default icon with custom color)
- Add path or polygon, with a parametric line width and color and a parametric filling color

Three modes are available: `centered`, `bounded` and `automatic`.

> 📣 important: only image URLs are returned.
> Contrary to the other functions of this library, the static map functions do not perform any query to MapTiler Cloud API, instead they build the image URL for you to use in `<img>` elements.

### Map Styles

In the following static map functions, the `option` object features a `style` property that can be a string or one of the built-in style shorthand. Here is the full list:

- `MapStyle.STREETS`, reference style for navigation and city exploration
  - `MapStyle.STREETS.DARK` (variant)
  - `MapStyle.STREETS.LIGHT` (variant)
  - `MapStyle.STREETS.PASTEL` (variant)
- `MapStyle.OUTDOOR` reference style for adventure
- `MapStyle.WINTER` reference style for winter adventure
- `MapStyle.SATELLITE` reference style satellite and airborne imagery (no variants)
- `MapStyle.HYBRID` reference style satellite and airborne imagery with labels (no variants)
- `MapStyle.BASIC` reference style for minimalist design and general purpose
  - `MapStyle.BASIC.DARK` (variant)
  - `MapStyle.BASIC.LIGHT` (variant)
- `MapStyle.BRIGHT` reference style for high contrast navigation
  - `MapStyle.BRIGHT.DARK` (variant)
  - `MapStyle.BRIGHT.LIGHT` (variant)
  - `MapStyle.BRIGHT.PASTEL` (variant)
- `MapStyle.TOPO` reference style for topographic study
  - `MapStyle.TOPO.SHINY` (variant)
  - `MapStyle.TOPO.PASTEL` (variant)
  - `MapStyle.TOPO.TOPOGRAPHIQUE` (variant)
- `MapStyle.VOYAGER` reference style for stylish yet minimalist maps
  - `MapStyle.VOYAGER.DARK` (variant)
  - `MapStyle.VOYAGER.LIGHT` (variant)
  - `MapStyle.VOYAGER.VINTAGE` (variant)
- `MapStyle.TONER` reference style for very high contrast stylish maps
  - `MapStyle.TONER.BACKGROUND` (variant)
  - `MapStyle.TONER.LITE` (variant)
  - `MapStyle.TONER.LINES` (variant)
- `MapStyle.OPENSTREETMAP` (reference style, this one does not have any variants)
- `MapStyle.DATAVIZ`, the perfect style for data visualization, with very little noise
  - `MapStyle.DATAVIZ.DARK` (variant)
  - `MapStyle.DATAVIZ.LIGHT` (variant)

### Centered static maps

This type of map is centered on a longitude-latitude coordinate and the zoom level must also be provided (from `0`: very zoomed out, to `22`: very zoomed in).
Note that if a path or markers are provided, the framing of the map will not automatically adapt to include those (use the `automatic` mode for that).

    const imageLink = maptilerClient.staticMaps.centered(
      // center position (Boston)
      [-71.06080, 42.362114],

      // zoom level
      12.5,

      // Options
      {
        // Request a hiDPI/Retina image
        hiDPI: true,

        // Output image size
        width: 1000,
        height: 1000,

        // Map style
        style: maptilerClient.MapStyle.OUTDOOR,
      });

Read more about centered static maps on our official API documentation.

### Bounded static maps

This type of map requires a bounding box made of two points: the south-west bound and the north-east bound. The zoom level cannot be provided and is automatically deduced from the size of the bounding box.

    const imageLink = maptilerClient.staticMaps.bounded(
      // The bounding box on Europe
      [
        -24,  // west bound (min x)
        34.5, // south bound (min y)
        32,   // east bound (max x)
        71,   // north bound (max y)
      ],

      // Options
      {
        hiDPI: true,
        width: 2048,
        height: 2048,
        style: maptilerClient.MapStyle.STREETS.DARK,

        // Extra space that will add around the bounding box, in percentage
        // (0.1 = 10% is actually the dafault)
        padding: 0.1
      });

Since the zoom level cannot be provided, the level of details is dictated by the size of the output image. here is an example:

`2048 x 2048` | `1024 x 1024`
--- | ---

As you may notice, the geo bounding box could have very different proportions than the output image size. In the following example, we place the very same bounding box around Portugal, which has a this particular strip looking shape. We also add a `path` that repeats exactly the same bounding box to show the difference between the provided bounding box and the final image. We kept the default padding of 10%:

`2048 x 2048` | `1024 x 2048`
--- | ---

Read more about bounded static maps on our official API documentation.

### Automatic static maps

As we have seen with centered and bounded maps, providing all the parameters is nice but can be cumbersome for the simplest use cases. This is why MapTiler Cloud also provides static maps that fits automatically whatever you need to place inside: path or markers.

In the following example, we are going to load a cycling track recorded by one of our team members in Montreal, Canada. The track, originally a GPX file, was pushed to MapTiler Data and is now made available as a GeoJSON:

    // Fetching the GeoJSON
    const bikeTrack = await maptilerClient.data.get('the-id-of-a-bike-track-in-montreal');

    // Extracting the track points with the shape [[lng, lat], [lng, lat], ...]
    const trackPoints = bikeTrack.features[0].geometry.coordinates[0]
      .map(p => p.slice(0, 2));

    const imageLink = maptilerClient.staticMaps.automatic({
      // hiDPI/Retina precision
      hiDPI: true,

      // A fairly large output image
      width: 2048,
      height: 1024 ,

      // A grey style on which the track will pop!
      style: maptilerClient.MapStyle.STREETS.LIGHT,

      // Draw a path with the trackpoints
      path: trackPoints,

      // Adding a marker for the starting point, with a custom color (array of shape [lng, lat, color])
      marker: [trackPoints[0][0], trackPoints[0][1], '#0a0'],

      // Showing the track in red
      pathStrokeColor: 'red',
    });

And voila!

> 📣 Note: The GeoJSON for this track contains 9380 couples of coordinates, which is a lot! In order to send the track to MapTiler Cloud static maps API, the client simplifies the long paths while keeping a high degree of precision using a very fast Ramer-Douglas-Peucker algorithm.

Read more about bounded static maps on our official API documentation.

# From NodeJS

NodeJS includes a stable `fetch()` function only from its version 18, and this client does not contain a polyfill. If the `fetch()` function exists (browser or Node >= 18) then it is going to be resolved automatically, Yet, a custom `fetch()` function can be provided to the `config` object for Node < 18.

In this NodeJS example, you can see that the package Node Fetch has been `npm install`ed and is passed to the config object of the MapTiler Client.

    import {
      config,
      // ...
    } from '@maptiler/client';

    // For this example to work, you must bring your own node-compatible fetch,
    // unles you are using a version of Nodejs that already contains fetch (>=18)
    import fetch from 'node-fetch';

    config.fetch = fetch;

    // ...

# Terms and usage limitations

The data fetched from MapTiler Cloud API, with or without this library, cannot be stored or redistributed in any ways. If you have any doubt about your specific usecase, please consult our legal terms or contact us.

Generated using TypeDoc