Skip to content
Change Building Color Based on Zoom Level

Change Building Color Based on Zoom Level

Apply zoom-dependent color expressions to existing map layers.

const API_KEY = 'toursprung';

    const map = new maptoolkit.Map({
        container: 'map',
        style: `https://static.maptoolkit.net/styles/toursprung/terrain.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', () => {
        map.setFilter('building', ['any', ['!has', 'level'], ['>=', 'level', 0]]);

        map.setPaintProperty('building', 'fill-color', [
            'interpolate',
            ['exponential', 0.5],
            ['zoom'],
            15, '#e2714b',
            22, '#eee695'
        ]);

        map.setPaintProperty('building', '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.2/dist/maptoolkit.js"></script>
    <link rel="stylesheet" href="https://unpkg.com/@maptoolkit/maps@11.0.0-beta.2/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 = 'toursprung';

    const map = new maptoolkit.Map({
        container: 'map',
        style: `https://static.maptoolkit.net/styles/toursprung/terrain.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', () => {
        map.setFilter('building', ['any', ['!has', 'level'], ['>=', 'level', 0]]);

        map.setPaintProperty('building', 'fill-color', [
            'interpolate',
            ['exponential', 0.5],
            ['zoom'],
            15, '#e2714b',
            22, '#eee695'
        ]);

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

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