Learn how to use a View with map components and Calcite components, to create an application with a header, thumbnail and web map. The key aspect here is to show how to use map components, and then access the created map in your script code.
In this tutorial, you will load and display a pre-configured web map stored in ArcGIS Online, and use Calcite Components to lay out your application with a header and logo at top, and the map below. The title and logo to display in the header will come from the web map item. You will also learn to display a loading indicator until the map is ready.
Steps
Create a new pen
- Go to CodePen to create a new pen for your mapping application.
Add basic HTML
Define a basic HTML page.
- In CodePen > HTML, add HTML to create a basic page.
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no" />
<title>
ArcGIS Maps SDK for JavaScript Tutorials: Using a View with map components and Calcite
components.
</title>
<style>
html,
body {
height: 100%;
margin: 0;
}
</style>
</head>
<body>
</body>
</html>Add references
- In the
<headtag, add references to the ArcGIS core library and CSS, Calcite components and map components packages.>
<!-- Load Calcite components from CDN -->
<script type="module" src="https://js.arcgis.com/calcite-components/3.3.3/calcite.esm.js"></script>
<!-- Load the ArcGIS Maps SDK for JavaScript from CDN -->
<script src="https://js.arcgis.com/4.34/"></script>
<!-- Load Map components from CDN-->
<script type="module" src="https://js.arcgis.com/4.34/map-components/"></script>
Create layout using Calcite
- Add the
<calcite-shellcomponent.> - Add the
<calcite-navigationcomponent, placing it in the shell's> headerslot. - Add the
<calcite-navigation-logocomponent, placing it in the> logoslot of the<calcite-navigation.> - Add the
<calcite-loadercomponent below the> <calcite-shellcomponent.>
<calcite-shell>
<calcite-navigation slot="header">
<calcite-navigation-logo slot="logo" target="_blank"></calcite-navigation-logo>
</calcite-navigation>
</calcite-shell>
<calcite-loader></calcite-loader>
Add arcgis-map component
- Add the
<arcgis-mapcomponent after the> <calcite-navigationcomponent.> - Add the
<arcgis-legendcomponent inside the> <arcgis-mapcomponent.>
<calcite-shell>
<calcite-navigation slot="header">
<calcite-navigation-logo slot="logo" target="_blank"></calcite-navigation-logo>
</calcite-navigation>
<arcgis-map item-id="05e015c5f0314db9a487a9b46cb37eca">
<arcgis-legend slot="bottom-right"></arcgis-legend>
</arcgis-map>
</calcite-shell>
<calcite-loader></calcite-loader>
Add script logic
- Add a
<scriptsection in the> <body.> - Add an eventListener to wait for when the "view" of the Map component is ready.
- Create variables for the view and the portal item used to create it.
- Set the heading, description and thumbnail of the
calcite-navigation-logo.
<script type="module">
const viewElement = document.querySelector("arcgis-map");
viewElement.addEventListener("arcgisViewReadyChange", (event) => {
const { portalItem } = viewElement.map;
const navigationLogo = document.querySelector("calcite-navigation-logo");
navigationLogo.heading = portalItem.title;
navigationLogo.description = portalItem.snippet;
navigationLogo.thumbnail = portalItem.thumbnailUrl;
navigationLogo.href = portalItem.itemPageUrl;
navigationLogo.label = "Thumbnail of map";
});
</script>
Hide loader
- Hide the loader once the view is ready
<script type="module">
const viewElement = document.querySelector("arcgis-map");
viewElement.addEventListener("arcgisViewReadyChange", (event) => {
const { portalItem } = viewElement.map;
const navigationLogo = document.querySelector("calcite-navigation-logo");
navigationLogo.heading = portalItem.title;
navigationLogo.description = portalItem.snippet;
navigationLogo.thumbnail = portalItem.thumbnailUrl;
navigationLogo.href = portalItem.itemPageUrl;
navigationLogo.label = "Thumbnail of map";
// BONUS: set ARIA properties on the map component for accessibility
viewElement.aria = {
label: portalItem.title,
description: portalItem.snippet,
};
// turn off the loader once the view is ready
document.querySelector("calcite-loader").hidden = true;
});
</script>
Run the app
In CodePen, run your code to display the map.
The app should display a map showing predominant educational attainment in New York, with a thumbnail and title at the top.
What's next?
- Go to the component reference for more detailed information about components.