ズーム・レベルに応じて建物の色を変更
補間式を使用して建物レイヤーをease-inし、色から色への変化をスムーズにします。
<!DOCTYPE html><html><head><meta charset="utf-8"><title>ズーム・レベルに応じて建物の色を変更</title><meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no"><link href="https://api.mapbox.com/mapbox-gl-js/v2.14.1/mapbox-gl.css" rel="stylesheet"><script src="https://api.mapbox.com/mapbox-gl-js/v2.14.1/mapbox-gl.js"></script><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> // TO MAKE THE MAP APPEAR YOU MUST // ADD YOUR ACCESS TOKEN FROM // https://account.mapbox.com mapboxgl.accessToken = 'YOUR_MAPBOX_ACCESS_TOKEN';const map = new mapboxgl.Map({container: 'map', // container IDstyle: 'mapbox://styles/mapbox/streets-v11', // style URLcenter: [-90.73414, 14.55524], // starting position [lng, lat]zoom: 15 // starting zoom}); map.on('load', () => {map.setPaintProperty('building', 'fill-color', ['interpolate',// Set the exponential rate of change to 0.5['exponential', 0.5],['zoom'],// When zoom is 15, buildings will be beige.15,'#D9D3C9',// When zoom is 18 or higher, buildings will be yellow.18,'#ffd700']); map.setPaintProperty('building', 'fill-opacity', ['interpolate',// Set the exponential rate of change to 0.5['exponential', 0.5],['zoom'],// When zoom is 10, buildings will be 100% transparent.10,0.5,// When zoom is 18 or higher, buildings will be 100% opaque.18,1]);}); // When the button is clicked, zoom in to zoom level 19.// The animation duration is 9000 milliseconds.document.getElementById('zoom').addEventListener('click', () => {map.zoomTo(18, { duration: 9000 });});</script> </body></html>