MapsGL - Customizing alert polygon styles
This example customizes the alert colors of the alerts weather layer by adjusting the color of the alert polygons based on the severity/type of each alert. This type of implementation can be useful when you want to highlight warnings vs watches vs advisories and statements.
custom-alert-styles.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>MapsGL SDK - Custom alert styles</title>
<meta name="description" content="Use custom alert layer styles to show different alert levels." />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link href="https://api.mapbox.com/mapbox-gl-js/v3.12.0/mapbox-gl.css" rel="stylesheet" />
<script defer src="https://api.mapbox.com/mapbox-gl-js/v3.12.0/mapbox-gl.js"></script>
<link href="https://cdn.aerisapi.com/sdk/js/mapsgl/1.10.1/aerisweather.mapsgl.css" rel="stylesheet" />
<script defer src="https://cdn.aerisapi.com/sdk/js/mapsgl/1.10.1/aerisweather.mapsgl.js"></script>
<style>
body, html {
margin: 0;
padding: 0;
}
#map {
height: 100vh;
width: 100%;
}
</style>
</head>
<body>
<div id="map"></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: [-96.33207, 40.60621],
zoom: 3
});
const account = new aerisweather.mapsgl.Account('CLIENT_ID', 'CLIENT_SECRET');
const controller = new aerisweather.mapsgl.MapboxMapController(map, { account });
const alertColor = [
'case',
['regex', 'warning|extreme', 'i', ['get', 'ADVISORY']], '#df1616',
['regex', 'watch|severe', 'i', ['get', 'ADVISORY']], '#ff9600',
['regex', 'advisory|moderate', 'i', ['get', 'ADVISORY']], '#009ac8',
['regex', 'statement|alert|minor', 'i', ['get', 'ADVISORY']], '#808080',
['coalesce', ['get', 'COLOR'], '#000']
];
controller.on('load', () => {
controller.addWeatherLayer('alerts', {
paint: {
fill: {
color: alertColor,
opacity: 0.5
},
stroke: {
color: alertColor,
opacity: 1
}
}
});
});
});
</script>
</body>
</html>