Customizing the heat index legend
This example updates the heat index legend after 5 seconds to display custom labels based on a normalized range of heat index values (from 0 to 1).
custom-heat-index-legend.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>MapsGL SDK - Customizing the heat index legend</title>
<meta name="description" content="Update the default heat index legend with custom labels." />
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://api.mapbox.com/mapbox-gl-js/v2.8.0/mapbox-gl.css" rel="stylesheet" />
<script defer src="https://api.mapbox.com/mapbox-gl-js/v2.8.0/mapbox-gl.js"></script>
<link href="https://cdn.aerisapi.com/sdk/js/mapsgl/latest/aerisweather.mapsgl.css" rel="stylesheet" />
<script defer src="https://cdn.aerisapi.com/sdk/js/mapsgl/latest/aerisweather.mapsgl.js"></script>
<style>
body, html {
margin: 0;
padding: 0;
}
#map {
height: 100vh;
width: 100%;
}
</style>
</head>
<body>
<div id="map"></div>
<div id="legend"></div>
<script>
window.addEventListener('load', () => {
mapboxgl.accessToken = 'MAPBOX_TOKEN';
const map = new mapboxgl.Map({
container: document.getElementById('map'),
style: 'mapbox://styles/mapbox/light-v9',
center: [-93, 34],
zoom: 3
});
const account = new aerisweather.mapsgl.Account('CLIENT_ID', 'CLIENT_SECRET');
const controller = new aerisweather.mapsgl.MapboxMapController(map, { account });
controller.on('load', () => {
controller.addLegendControl('#map');
controller.addWeatherLayer('heat-index');
setTimeout(() => {
const legend = controller.controls.legend.getLegend('heat-index');
legend.update({
colorscale: {
labels: {
normalized: true,
values: [
{ value: 0, label: 'Uncomfortable' },
{ value: 0.5, label: 'Hot' },
{ value: 1, label: 'Dangerous' }
],
marks: 'none'
}
}
});
}, 5000);
});
});
</script>
</body>
</html>