Add a new layer below labels

By utilizing the second parameter of the addLayer function in the SDK-JS API, you can enhance the accuracy of adding a new layer beneath the labels or any other desire layer. This allows for more precise placement and control over the layer’s position in relation to the rest of the map layers.

NPM module setup


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

config.apiKey = 'YOUR_MAPTILER_API_KEY_HERE';
const map = new Map({
  container: 'map',
  style: MapStyle.STREETS, 
  center: [-88.13734351262877, 35.137451890638886],
  zoom: 4
});

map.on('load', function () {
  const layers = map.getStyle().layers;
  // Find the index of the first symbol layer in the map style
  // This approach works with our simple map styles for more advanced styles, e.g., Outdoor.
  // We recommend directly inserting the name of the last layer above your data. 
  // See the Learn more section below.
  const firstSymbolId = layers.find(layer => layer.type === 'symbol').id;

  map.addSource('urban-areas', {
    'type': 'geojson',
    'data':
      'https://d2ad6b4ur7yvpq.cloudfront.net/naturalearth-3.3.0/ne_50m_urban_areas.geojson'
  });
  map.addLayer(
    {
      'id': 'urban-areas-fill',
      'type': 'fill',
      'source': 'urban-areas',
      'layout': {},
      'paint': {
        'fill-color': '#f08',
        'fill-opacity': 0.4
      }
      // This is the important part of this example: the addLayer
      // method takes 2 arguments: the layer as an object, and a string
      // representing another layer's name. if the other layer
      // exists in the stylesheet already, the new layer will be positioned
      // right before that layer in the stack, making it possible to put
      // 'overlays' anywhere in the layer stack.
      // Insert the layer beneath the first symbol layer.
    },
    firstSymbolId
  );
});

Learn more

In this example, we show how to find the Id of the first symbol layer in the map style.

Sometimes, we want to get the id of a layer other than the first symbol layer. For example, the Outdoor style has contour line labels below some polygons (intentionally cartographic hack), but we don’t want to add our layer before the contour line labels.

To find the id of the layer that will be used before my visualization layer you can search for it directly in the style json or more easily using the Map Designer tool.

  1. Go to MapTiler

  2. Select the map you are using in your application

  3. Click the Customize button

  4. Go to layer - Verticality and find the layer

  5. Click at the layer and open JSON editor ( {} )

  6. Copy layer id e.g. “River labels”

MapTiler Customize get layer ID

Icon image URL inside a rich text format label

Icon image URL inside a rich text format label

Examples

Display rich text labels with texts and icons.

Show raster image on the map

Raster layer

Tutorials

Add a raster image overlay to the map.

Add a video

Add a video

Examples

Display videos overlaying a satellite raster baselayer on maps.

Add a WMS source

Add a WMS source

Examples

Add an external Web Map Service raster layer.

Was this helpful?