One of the things that makes the new AerisWeather JavaScript SDK so unique is its versatility. The SDK is your new go-to for weather data, static maps, and even interactive maps. This post will highlight how easy it is to use an interactive map and include layer buttons with our JavaScript SDK.
Just like the fetching data and static maps posts related to our JavaScript SDK, we will need to get our feet set with a few items before we can begin. Let’s set up the following items:
You should have something that looks like that after going through the three steps above:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>AerisJS Interactive Maps</title> <link rel="stylesheet" type="text/css" href="./aerisjs-int-map-styles.css"> </head> <body> <script src="https://cdn.aerisapi.com/sdk/js/latest/aerisweather.min.js" defer></script> <link rel="stylesheet" href="https://cdn.aerisapi.com/sdk/js/latest/aerisweather.css"> <div id="map-container"> <div id="int-map"></div> <div id="btn-list"> <button id="radar" class="layer-btn">Radar</button> <button id="alerts" class="layer-btn">Alerts</button> <button id="satellite" class="layer-btn">Satellite</button> </div> </div> <script> window.onload = function() { const aeris = new AerisWeather('CLIENT_ID', 'CLIENT_SECRET'); } </script> </body> </html> |
Now we’re ready to move on and start using the library.
In the previous snippet, we instantiated the JS library and assigned the instance to a local variable named aeris. Now we’re going to use the views() method to access the mapping features found within the Views module of the SDK. The JavaScript SDK has Promises built in so we can instantly start using view-related components within the callback passed to the Promise’s then() function:
1 2 3 |
aeris.views().then(views => { // config will go here }); |
The initial config for our interactive map will customize the center, zoom level, and strategy. Center and zoom are self-explanatory, but strategy is another example of the JavaScript SDK’s versatility. Here you have the option to choose between 4 popular interactive map library: Google Maps, Mapbox, Leaflet, and OpenLayers. For this exercise, we’ll be using Leaflet which is also the default strategy. Here’s what the interactive map setup would look like using our custom configuration options:
1 2 3 4 5 6 7 8 9 10 11 |
aeris.views().then(views => { const mapTarget = document.getElementById('int-map'); const map = new views.InteractiveMap(mapTarget, { center: { lat: 40, lon: -100 }, zoom: 4, strategy: 'leaflet' }); }); |
The interactive map is set up, but I still need to add weather layers. However, I don’t want to just add a bunch of layers; I want the user to be able to turn weather layers on and off. In the last code snippet, we assigned our InteractiveMap instance to a local map variable to access later. We will need to take that map and add the toggling functionality after the map loads:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
aeris.views().then(views => { const mapTarget = document.getElementById('int-map'); const map = new views.InteractiveMap(mapTarget, { center: { lat: 40, lon: -100 }, zoom: 4, strategy: 'leaflet' }); // wait for the load to be fully initialized by the underlying // mapping strategy before trying to work with it map.on('load', () => { // add toggling functionality here }); }); |
Now the question is what do we need to toggle? I’d like to toggle two components at this step:
First, let’s store the DOM targets for our buttons in an array:
1 2 3 4 5 |
const allButtons = [ document.getElementById('radar'), document.getElementById('alerts'), document.getElementById('satellite') ]; |
Next, we can take all of those buttons and add the same event handler when the ‘click’ event takes place:
1 2 3 |
allButtons.forEach(btn => { btn.addEventListener('click', toggleBtn); }); |
Now we just need to build out the toggleBtn function which received a single argument, which is an Event object associated with the triggered event. Within that function, we use the addLayer() and removeLayer() methods on our InteractiveMap instance to toggle the visibility of those layers based on the selected state of the button. The presence of my selected button class, .selected, will be used to determine whether or not the layer needs to be added or removed. We also want to be cognizant of the order in which the layers are added as I don’t want my radar layer underneath my satellite layer. Therefore, I will add logic to determine which layer I’m working with so I can set my z-index accordingly. Here’s the entire function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
function toggleBtn(e) { const clickedBtn = e.target; const btnLayer = clickedBtn.id; let zIndex; switch(btnLayer) { case 'alerts': zIndex = 1; break; case 'radar': zIndex = 3; break; case 'satellite': zIndex = 2; break; } if (e.target.className.includes('selected')) { console.log(<pre class="inline:true decode:1 " >remove layer ${btnLayer} |
);
map.removeLayer(btnLayer);
} else {
console.log(
add layer ${btnLayer});
map.addLayer(btnLayer, {
style: {
zIndex: zIndex
}
});
}
clickedBtn.classList.toggle(‘selected’);
}
Be sure to check out the complete code example that you can download and review as well.
That should wrap up all the individual pieces to build our your application. At this point, you may want to consider adding some styling to your applications, but I’ll leave that to the design experts. Now your users will be able to decide which layer(s) they want to see on an interactive map as the next big storm rolls in. Start using the JavaScript SDK today; we can’t wait to see what you build!
No comments yet.
Be the first to respond to this article.