Skip to content
Change Building Color Based on Zoom Level

Change Building Color Based on Zoom Level in Maptoolkit Maps JS

Zoom-dependent styles in Maptoolkit Maps JS use interpolate expressions to transition a paint property smoothly between values as the zoom level changes. This example applies a color ramp to the 3D building extrusion layer so buildings shift from a neutral tone at low zoom to a highlighted color when zoomed in. Use this technique to create context-aware styles that reveal detail progressively as the user zooms, without needing separate layers for each zoom range.

const API_KEY = 'YOUR_API_KEY';

    const map = new maptoolkit.Map({
        container: 'map',
        apiKey: API_KEY,
        style: `https://styles.maptoolkit.net/maptoolkit/maptoolkit.summer.json?api_key=${API_KEY}`,
        center: [2.3488, 48.8534],
        zoom: 13,
        attributionControl: { compact: false }
    });

    map.addControl(new maptoolkit.NavigationControl(), 'top-right');

    map.on('load', () => {
        ['building_base', 'building_multicolored'].forEach((id) => {
            map.setPaintProperty(id, 'fill-color', [
                'interpolate',
                ['exponential', 0.5],
                ['zoom'],
                15, '#e2714b',
                22, '#eee695'
            ]);

            map.setPaintProperty(id, 'fill-opacity', [
                'interpolate',
                ['exponential', 0.5],
                ['zoom'],
                15, 0,
                22, 1
            ]);
        });
    });

    document.getElementById('zoom').addEventListener('click', () => {
        map.zoomTo(19, { duration: 9000 });
    });
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Change Building Color Based on Zoom Level - Maptoolkit Maps JS</title>
    <meta property="og:description" content="Apply zoom-dependent color expressions to existing map layers." />
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script src="https://unpkg.com/@maptoolkit/maps@11.0.0-beta.3/dist/maptoolkit.js"></script>
    <link rel="stylesheet" href="https://unpkg.com/@maptoolkit/maps@11.0.0-beta.3/dist/maptoolkit.css" />
    <style>
        html, body { width: 100%; height: 100%; margin: 0; padding: 0; }
        #map { width: 100%; height: 100%; }
        #zoom {
            display: block;
            position: absolute;
            top: 20px;
            left: 50%;
            transform: translate(-50%);
            width: 50%;
            height: 40px;
            padding: 10px;
            border: none;
            border-radius: 3px;
            font-size: 12px;
            text-align: center;
            color: #fff;
            background: #ee8a65;
            cursor: pointer;
        }
        #zoom:hover { background: #d4784f; }
    </style>
</head>
<body>
<div id="map"></div>
<button id="zoom">Zoom to buildings</button>
<script>
    const API_KEY = 'YOUR_API_KEY';

    const map = new maptoolkit.Map({
        container: 'map',
        apiKey: API_KEY,
        style: `https://styles.maptoolkit.net/maptoolkit/maptoolkit.summer.json?api_key=${API_KEY}`,
        center: [2.3488, 48.8534],
        zoom: 13,
        attributionControl: { compact: false }
    });

    map.addControl(new maptoolkit.NavigationControl(), 'top-right');

    map.on('load', () => {
        ['building_base', 'building_multicolored'].forEach((id) => {
            map.setPaintProperty(id, 'fill-color', [
                'interpolate',
                ['exponential', 0.5],
                ['zoom'],
                15, '#e2714b',
                22, '#eee695'
            ]);

            map.setPaintProperty(id, 'fill-opacity', [
                'interpolate',
                ['exponential', 0.5],
                ['zoom'],
                15, 0,
                22, 1
            ]);
        });
    });

    document.getElementById('zoom').addEventListener('click', () => {
        map.zoomTo(19, { duration: 9000 });
    });
</script>
</body>
</html>

Use the prompt below with any LLM to get the same result. Make sure the Maptoolkit MCP server is connected first — check out AI Integration & MCP to get started.

Use the Maptoolkit Connector. Create an interactive map with zoom level 13, centered around [2.3488, 48.8534]. Apply a zoom-dependent color interpolation to the building layer and add a button that smoothly zooms to level 19.

How it works

['interpolate', ['exponential', 0.5], ['zoom'], 15, colorA, 22, colorB] blends between two colours as the zoom changes. The renderer evaluates it per frame, so the transition is smooth rather than stepping at each integer zoom.

The interpolation type matters. ['exponential', 0.5] weights the change toward the lower end of the range, where ['linear'] spreads it evenly. Zoom is itself exponential in scale, so a linear ramp over zoom often looks weighted toward the top.

['zoom'] is only valid at the top level of an interpolate expression in a paint property. It cannot be nested inside another expression, which is a common cause of a style that fails to parse.

The example targets building_base and building_multicolored, which are ids from our styles. Check the layer exists before setting a property on it, since ids differ between styles.

Next steps

Zoom-driven styling applies to far more than colour. Line widths, label sizes, opacity and icon sizes all usually need it, and a map that looks right at one zoom and cluttered at another is almost always missing an interpolation.

Zoom-dependent visibility is the related tool: minzoom and maxzoom on a layer bring detail in as the user gets closer, which is how the basemap itself stays readable across the range.