How to take a screenshot of the current map view (screenshot helper)

This example shows how to capture the current map view as a blob (PNG image). The screenshot helper is the most convenient way to save map snapshots.

The returned blob of a PNG image file can be very handy if the goal is to programmatically further manipulate the screenshot, such as sending it to some feedback endpoint with a POST request.

In this example we are going to use the takeScreenshot helper to replace the drawn map image (in the lower left corner of the map) with an image of the current map view.

Click on the Take screenshot button to replace the drawing map image for the current map view image

NPM module setup


npm install --save @maptiler/sdk
import { Map, MapStyle, config, helpers } from '@maptiler/sdk';
import '@maptiler/sdk/dist/maptiler-sdk.css';

config.apiKey = 'YOUR_MAPTILER_API_KEY_HERE';
const map = new Map({
    container: 'map',
    zoom: 17.5,
    center: [16.373344, 48.208479],
    style: MapStyle.HYBRID
});

map.on('ready', () => {
  document.getElementById('screenshot').addEventListener('click', function () {
    takePicture();
  });
});

async function takePicture() {
  const imageSnapshot = document.getElementById('snapshot');
  imageSnapshot.classList.add('hidden');
  const mapBlob = await helpers.takeScreenshot(map);
  const objectURL = URL.createObjectURL(mapBlob);
  imageSnapshot.src = objectURL;
  imageSnapshot.classList.remove('hidden');
}
How to download a map screenshot as a PNG file (screenshot helper)

Download a map screenshot (screenshot helper)

Examples

Download map screenshot as PNG file using helper.

Heatmap layer (heatmap helper)

Heatmap layer (heatmap helper)

Examples

Quickly add a heatmap layer using the heatmap helper.

Point layer labels (point helper)

Point layer labels (point helper)

Examples

Add labeled point layer using MapTiler point helper.

Point layer scaled radius by property (point helper)

Point layer scaled radius by property (point helper)

Examples

Scale point layer radius and color by property.

Was this helpful?