Goong DocumentsExamplesChange building color based on zoom level

Change building color based on zoom level

Use the interpolate expression to ease-in the building layer and smoothly fade from one color to the next.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Change building color based on zoom level</title>
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
<script src="https://cdn.jsdelivr.net/npm/@goongmaps/goong-js@1.0.9/dist/goong-js.js"></script>
<link href="https://cdn.jsdelivr.net/npm/@goongmaps/goong-js@1.0.9/dist/goong-js.css" rel="stylesheet" />
<style>
body { margin: 0; padding: 0; }
#map { position: absolute; top: 0; bottom: 0; width: 100%; }
</style>
</head>
<body>
<style>
#zoom {
display: block;
position: relative;
margin: 20px auto;
width: 50%;
height: 40px;
padding: 10px;
border: none;
border-radius: 3px;
font-size: 12px;
text-align: center;
color: #fff;
background: #ee8a65;
}
</style>
<div id="map"></div>
<button id="zoom">Zoom to buildings</button>
<script>
goongjs.accessToken = '8qzxZAuxcsctSlmOszInchP1A5GrmRBHJwCBCjO6';
var map = new goongjs.Map({
container: 'map',
style: 'https://tiles.goong.io/assets/goong_map_web.json',
center: [105.83991, 21.028],
zoom: 13
});
map.on('load', function () {
map.addLayer({
'id': 'building',
'source': 'composite',
'source-layer': 'VN_Building',
'filter': ['==', 'extrude', 'true'],
'type': 'fill-extrusion',
'minzoom': 15,
'paint': {
'fill-extrusion-color': '#aaa',
// use an 'interpolate' expression to add a smooth transition effect to the
// buildings as the user zooms in
'fill-extrusion-height': [
'interpolate',
['linear'],
['zoom'],
15,
0,
15.05,
['get', 'height']
],
'fill-extrusion-base': [
'interpolate',
['linear'],
['zoom'],
15,
0,
15.05,
['get', 'min_height']
],
'fill-extrusion-opacity': 0.6
}
});
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', function () {
map.zoomTo(19, { duration: 9000 });
});
</script>
</body>
</html>