Icon image URL inside a rich text format label

Use the Formatted type to display visually appealing text labels featuring both text and icons sourced from image URLs. This versatile type enables the presentation of rich content in a visually engaging manner.

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', // container's id or the HTML element to render the map
  style: MapStyle.DATAVIZ.LIGHT,
  center: [-93.96, 38.14],
  zoom: 4.58
});

map.on('load', async function () {

  map.addSource('states', {
    'type': 'geojson',
    'data': 'https://docs.maptiler.com/sdk-js/assets/us_states.geojson'
  });

  map.addLayer({
    'id': 'state-fills',
    'type': 'fill',
    'source': 'states',
    'layout': {},
    'paint': {
        'fill-color': "#D3DBEC",
        'fill-opacity': 0.8,
        'fill-outline-color': "rgba(255, 255, 255, 1)"
    }
  });

  map.addLayer({
    'id': 'labels',
    'type': 'symbol',
    'source': 'states',
    'layout': {
      'text-field': ["format",
        ["image", ['downcase', ['get', 'ABBREVIATION']]],
        '\n',
        ['get', 'STATE_NAME'], { "font-scale": 0.8 },
        '\n',
        ['get', 'ABBREVIATION'], { "font-scale": 0.7, "text-color": "#444952" },
        '\n',
        "People/Km2: ", { "font-scale": 0.6 },
        ['get', 'DENSITY'], { "font-scale": 0.6, "text-color": "#0E61C6", "text-font": [
              'literal',
              ['Noto Sans Bold', 'Arial Unicode MS Bold']
          ]},
      ]
    }
  });
});

map.on("styleimagemissing", async function (evt) {
  const usid = evt.id;
  const image = await map.loadImage(`https://docs.maptiler.com/sdk-js/examples/rich-text-format/img/${usid}.webp`);
  if (!map.hasImage(`${usid}`)) map.addImage(`${usid}`, image.data);
});
Change the case of labels

Change the case of labels

Examples

Modify label text casing using upcase and downcase expressions.

Display and style rich text labels

Display and style rich text labels

Examples

Display multilingual rich text labels using format expressions.

How to change the default map labels language

Set map language

Tutorials

This tutorial shows how to change the default map labels language.

Add a stretchable image to the map

Add a stretchable image to the map

Examples

Use stretchable images as background for map text labels.

Was this helpful?