Skip to content
Change Building Color Based on Zoom Level

Change Building Color Based on Zoom Level

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/[email protected]/dist/maptoolkit.js"></script>
    <link rel="stylesheet" href="https://unpkg.com/@maptoolkit/[email protected]/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.