MapSnippets logo MapSnippets logo

3D Building Extrusions with MapTiler SDK

MapTiler SDK

Extrude 3D buildings on a MapTiler SDK map with a fill-extrusion layer that reads render_height from the building source layer of the vector tiles.

progress_activity Loading map...
maptilersdk.config.apiKey = 'YOUR_MAPTILER_KEY';
const map = new maptilersdk.Map({
  container: 'map',
  style: maptilersdk.MapStyle.STREETS,
  center: [2.3488, 48.8534],
  zoom: 15.5,
  pitch: 60,
  bearing: -17,
  canvasContextAttributes: { antialias: true }
});
map.on('load', () => {
  const style = map.getStyle();
  // Skip attribution-only vector sources (no url/tiles) such as maptiler_attribution.
  const src = Object.keys(style.sources).find(k =>
    style.sources[k].type === 'vector' && (style.sources[k].url || style.sources[k].tiles));
  if (!src) return;
  // Insert above basemap but below labels so labels stay on top.
  const labelLayerId = style.layers.find(l => l.type === 'symbol' && l.layout && l.layout['text-field'])?.id;
  map.addLayer({
    id: 'buildings-3d',
    type: 'fill-extrusion',
    source: src,
    'source-layer': 'building',
    minzoom: 14,
    filter: ['!=', ['get', 'hide_3d'], true],
    paint: {
      'fill-extrusion-color': '#9C27B0',
      'fill-extrusion-height': [
        'interpolate', ['linear'], ['zoom'],
        14, 0,
        16, ['coalesce', ['get', 'render_height'], 0]
      ],
      'fill-extrusion-base': ['coalesce', ['get', 'render_min_height'], 0],
      'fill-extrusion-opacity': 0.8
    }
  }, labelLayerId);
});

How to show 3D buildings with MapTiler SDK

MapTiler vector tiles include a building source layer with height attributes. The snippet detects the vector source at runtime rather than hardcoding a source ID, making it resilient to style changes.

MapTiler styles also contain an attribution-only vector source (maptiler_attribution) with no url or tiles. Because it comes first in the style, the snippet checks for url or tiles so the extrusion layer is attached to the source that actually serves building tiles.

The fill-extrusion layer type renders polygons with vertical height. The render_height and render_min_height properties from the building data define the top and base of each extrusion.

MapTiler SDK is built on MapLibre GL JS 5, so two protections from the MapLibre example apply here too. The ['!=', ['get', 'hide_3d'], true] filter excludes features the tiles mark as not-for-3D (without it you can hit a TypeError inside loadTile). The ['coalesce', ..., 0] wrapper defends against null render_height / render_min_height values.

Passing labelLayerId as the second argument to addLayer() inserts the 3D layer below the first label layer, so road and POI labels stay visible on top.

Set pitch and bearing in the constructor to view the 3D effect. Without pitch, extrusions are not visible.

Key APIs

API Description Docs
map.addLayer() Add a new layer to the style docs.maptiler.com/sdk-js/api/
fill-extrusion Layer type for 3D polygon rendering docs.maptiler.com/sdk-js/api/
map.getStyle() Get current style to inspect sources docs.maptiler.com/sdk-js/api/

FAQ

Why detect the source dynamically instead of hardcoding it?

MapTiler style source IDs can change between style versions. Runtime detection of the vector source that has url or tiles keeps the code working across MapTiler styles.

Can I color buildings by height?

Yes. Replace the static color with an expression like ['interpolate', ['linear'], ['get', 'render_height'], 0, '#aaa', 50, '#9C27B0'].

Common mistakes

  • Forgetting to set pitch, which leaves extrusions invisible from directly above
  • Hardcoding the source ID, which varies by style version
  • Picking the first type: 'vector' source. In MapTiler styles that is the attribution-only maptiler_attribution source with no tiles, so every tile load throws and the map goes blank
  • Adding the layer before the load event, when the sources are not available yet
  • Passing raw ['get', 'render_height'] without coalesce, so null values crash loadTile under MapLibre 5.x (which MapTiler SDK bundles)
  • Omitting the beforeId argument to addLayer(), so the extrusion covers labels and icons

Build maps using MapTiler SDK with AI

Generate code like this 3d building extrusions with maptiler sdk example using the MapTiler SDK skill in your IDE.