MapSnippets logoMapSnippets logo

Data-Driven Choropleth with MapTiler SDK

MapTiler SDK

Create a data-driven choropleth map using fill layer paint expressions with MapTiler SDK.

progress_activityLoading map...
maptilersdk.config.apiKey = 'YOUR_MAPTILER_KEY';
const map = new maptilersdk.Map({
  container: 'map',
  style: maptilersdk.MapStyle.STREETS,
  center: [15.0, 50.0],
  zoom: 4
});
map.on('load', () => {
  map.addSource('countries', {
    type: 'geojson',
    data: 'https://d2ad6b4ur7yvpq.cloudfront.net/naturalearth-3.3.0/ne_50m_admin_0_countries.geojson'
  });
  map.addLayer({
    id: 'countries-fill', type: 'fill', source: 'countries',
    paint: {
      'fill-color': ['interpolate', ['linear'], ['get', 'pop_est'],
        0, '#e8d5f5', 10000000, '#ce93d8', 100000000, '#9C27B0', 1000000000, '#4a148c'],
      'fill-opacity': 0.7
    }
  });
  map.addLayer({
    id: 'countries-border', type: 'line', source: 'countries',
    paint: { 'line-color': '#2a2a2a', 'line-width': 1 }
  });
});

How it works

A choropleth colors regions by a data value. The interpolate expression reads the pop_est property from each GeoJSON feature and maps it to a color gradient from light purple to dark purple.

The ['linear'] interpolation creates smooth color transitions. For discrete steps, use ['step', ['get', 'pop_est'], ...] instead.

A line layer adds borders between countries for visual clarity.

Key APIs

API Description Docs
interpolate expression Map numeric values to colors docs.maptiler.com/sdk-js/api/
['get', prop] Read a feature property docs.maptiler.com/sdk-js/api/
fill-color Data-driven paint property docs.maptiler.com/sdk-js/api/

FAQ

Can I add a legend?

Yes, but it must be custom HTML. The SDK does not have a built-in legend control. Add a positioned div with color swatches matching your expression stops.

How do I show a tooltip on hover?

Use map.on('mousemove', 'countries-fill', ...) to update a Popup with the hovered feature’s properties.

Common mistakes

  • Using match when interpolate is needed: match is for categorical data, interpolate for continuous numeric data
  • Forgetting fill-opacity: fully opaque fills hide the basemap labels underneath
  • Not adding a border layer: without borders, adjacent regions with similar colors blend together
auto_awesome

Build maps using MapTiler SDK with AI

Generate code like this data-driven choropleth with maptiler sdk example using the MapTiler SDK skill in your IDE.