Skip to content

Zoom-Based Building Color

Drive fill-extrusion-color with a zoom interpolation so extruded buildings shift color as the map zooms in. Building geometry comes from the Maptoolkit vector source - here the Midtown skyline seen from Central Park in New York City.

import { MaptoolkitLogoControl } from 'https://cdn.jsdelivr.net/npm/@maptoolkit/maplibre-gl-logo/+esm';
const map = new maplibregl.Map({ container: 'map', attributionControl: { customAttribution: '' }, style: 'https://styles.maptoolkit.org/summer.json', center: [-73.9770, 40.7645], zoom: 14.3, pitch: 62, bearing: 200 }); // Midtown Manhattan, looking out from Central Park South
map.on('load', function () {
  map.addLayer({ id: 'buildings-3d', type: 'fill-extrusion', source: 'mtk', 'source-layer': 'building', paint: {
    'fill-extrusion-color': ['interpolate', ['linear'], ['zoom'], 14, '#dddddd', 16, '#a0c4ff', 18, '#1a73e8'],
    'fill-extrusion-height': ['coalesce', ['get', 'render_height'], ['get', 'height'], 12],
    'fill-extrusion-base': ['coalesce', ['get', 'render_min_height'], ['get', 'min_height'], 0],
    'fill-extrusion-opacity': 0.9
  } });
});
map.addControl(new MaptoolkitLogoControl());
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/maplibre-gl.js"></script>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/maplibre-gl.css" />
  <style>
    html, body { width: 100%; height: 100%; margin: 0; padding: 0; }
    #map { width: 100%; height: 100%; }
  </style>
</head>
<body>
  <div id="map"></div>
  <script type="module">
import { MaptoolkitLogoControl } from 'https://cdn.jsdelivr.net/npm/@maptoolkit/maplibre-gl-logo/+esm';
const map = new maplibregl.Map({ container: 'map', attributionControl: { customAttribution: '' }, style: 'https://styles.maptoolkit.org/summer.json', center: [-73.9770, 40.7645], zoom: 14.3, pitch: 62, bearing: 200 }); // Midtown Manhattan, looking out from Central Park South
map.on('load', function () {
  map.addLayer({ id: 'buildings-3d', type: 'fill-extrusion', source: 'mtk', 'source-layer': 'building', paint: {
    'fill-extrusion-color': ['interpolate', ['linear'], ['zoom'], 14, '#dddddd', 16, '#a0c4ff', 18, '#1a73e8'],
    'fill-extrusion-height': ['coalesce', ['get', 'render_height'], ['get', 'height'], 12],
    'fill-extrusion-base': ['coalesce', ['get', 'render_min_height'], ['get', 'min_height'], 0],
    'fill-extrusion-opacity': 0.9
  } });
});
map.addControl(new MaptoolkitLogoControl());
  </script>
</body>
</html>