Learn how to create a web application with Map Components and Calcite components using Vite developer build tools.
In this tutorial, you will:
- Load and display a pre-configured
web map stored in ArcGIS Online with an arcgis-layer-list component. - Create an application layout and user interface with Calcite components.
- Add a map and a layer list leveraging Map Components.
- Learn how to use the core API library to interact with the View, Map, and layers.
Prerequisites
- The most recent LTS Node.js runtime environment.
- A text editor to edit files.
- A terminal to enter commands.
Steps
Create a new project using Vite
- Download the initial Vite vanilla Javascript project to your local machine.
- Unzip the downloaded file.
- Open the unzipped folder’s files in your text editor.
- Navigate to the unzipped folder in your terminal.
Install dependencies
-
To use map components, install the
@arcgis/map-componentspackage and its dependencies into your project:npm install @arcgis/map-componentsyarn add @arcgis/map-components @arcgis/core @esri/calcite-components -
Start the Vite development server.
npm run devyarn run dev -
Open a web browser and navigate to http://localhost:5173, this webpage will be empty, however, it will update as we proceed through the remaining steps.
-
Import the Layer List, Map and Zoom components from the
@arcgis/map-componentspackage inmain.js.main.jsimport "./style.css";import "@arcgis/map-components/components/arcgis-layer-list";import "@arcgis/map-components/components/arcgis-map";import "@arcgis/map-components/components/arcgis-zoom"; -
Import the Shell, Navigation and Navigation Logo components from the
@esri/calcite-componentspackage inmain.js.main.jsimport "./style.css";import "@arcgis/map-components/components/arcgis-layer-list";import "@arcgis/map-components/components/arcgis-map";import "@arcgis/map-components/components/arcgis-zoom";import "@esri/calcite-components/components/calcite-navigation";import "@esri/calcite-components/components/calcite-navigation-logo";import "@esri/calcite-components/components/calcite-shell";
Add styles
-
Add CSS styles to make html and body fill the entire viewport with no margin.
style.csshtml,body {height: 100%;margin: 0;}
Add components
-
To create an application layout and user interface for your app add
calcite-shell,calcite-navigation,calcite-navigation-logocomponents toindex.html.index.html9 collapsed lines<!doctype html><html lang="en"><head><meta charset="UTF-8" /><link rel="icon" href="data:;base64,=" /><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /><title>ArcGIS Maps SDK for JavaScript Tutorials: Create a web app using components</title></head><body><calcite-shell><calcite-navigation slot="header"><calcite-navigation-logo slot="logo"></calcite-navigation-logo></calcite-navigation></calcite-shell><script type="module" src="/main.js"></script></body>2 collapsed lines</html> -
Next, add the Map, Zoom and Layer List components.
index.html11 collapsed lines<!doctype html><html lang="en"><head><meta charset="UTF-8" /><link rel="icon" href="data:;base64,=" /><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /><title>ArcGIS Maps SDK for JavaScript Tutorials: Create a web app using components</title></head><body><calcite-shell><calcite-navigation slot="header"><calcite-navigation-logo slot="logo"></calcite-navigation-logo></calcite-navigation><arcgis-map item-id="e691172598f04ea8881cd2a4adaa45ba" zoom="4"><arcgis-zoom slot="top-left"></arcgis-zoom><arcgis-layer-list slot="top-right"></arcgis-layer-list></arcgis-map></calcite-shell>5 collapsed lines<script type="module" src="/main.js"></script></body></html>
Add a Legend to the LayerList
-
In
main.jsusedocument.querySelector()to get a reference for the Layer List component.main.js10 collapsed linesimport "./style.css";import "@arcgis/map-components/components/arcgis-layer-list";import "@arcgis/map-components/components/arcgis-map";import "@arcgis/map-components/components/arcgis-zoom";import "@esri/calcite-components/components/calcite-navigation";import "@esri/calcite-components/components/calcite-navigation-logo";import "@esri/calcite-components/components/calcite-shell";// Get a reference to the arcgis-layer-list elementconst arcgisLayerList = document.querySelector("arcgis-layer-list"); -
Add a listItemCreatedFunction to the Layer List. This function will add a Legend in the ListItemPanel for all layers except group layers.
main.js10 collapsed linesimport "./style.css";import "@arcgis/map-components/components/arcgis-layer-list";import "@arcgis/map-components/components/arcgis-map";import "@arcgis/map-components/components/arcgis-zoom";import "@esri/calcite-components/components/calcite-navigation";import "@esri/calcite-components/components/calcite-navigation-logo";import "@esri/calcite-components/components/calcite-shell";// Get a reference to the arcgis-layer-list elementconst arcgisLayerList = document.querySelector("arcgis-layer-list");// Set the listItemCreatedFunction to add a legend to each list itemarcgisLayerList.listItemCreatedFunction = (event) => {const { item } = event;if (item.layer.type !== "group") {item.panel = {content: "legend",};}};
Interacting with the View, Map, and layers
-
Use
document.querySelector()to get a reference for the Map component. Since we are going to use property values from the map component, we’ll use thearcgisViewReadyChangeevent to determine when the map is ready.main.js23 collapsed linesimport "./style.css";import "@arcgis/map-components/components/arcgis-layer-list";import "@arcgis/map-components/components/arcgis-map";import "@arcgis/map-components/components/arcgis-zoom";import "@esri/calcite-components/components/calcite-navigation";import "@esri/calcite-components/components/calcite-navigation-logo";import "@esri/calcite-components/components/calcite-shell";// Get a reference to the arcgis-layer-list elementconst arcgisLayerList = document.querySelector("arcgis-layer-list");// Set the listItemCreatedFunction to add a legend to each list itemarcgisLayerList.listItemCreatedFunction = (event) => {const { item } = event;if (item.layer.type !== "group") {item.panel = {content: "legend",};}};// Get a reference to the arcgis-map elementconst viewElement = document.querySelector("arcgis-map");// Wait for the map component to be ready before accessing its properties.viewElement.addEventListener("arcgisViewReadyChange", () => {}); -
Create a variable for the map’s PortalItem instance.
main.js26 collapsed linesimport "./style.css";import "@arcgis/map-components/components/arcgis-layer-list";import "@arcgis/map-components/components/arcgis-map";import "@arcgis/map-components/components/arcgis-zoom";import "@esri/calcite-components/components/calcite-navigation";import "@esri/calcite-components/components/calcite-navigation-logo";import "@esri/calcite-components/components/calcite-shell";// Get a reference to the arcgis-layer-list elementconst arcgisLayerList = document.querySelector("arcgis-layer-list");// Set the listItemCreatedFunction to add a legend to each list itemarcgisLayerList.listItemCreatedFunction = (event) => {const { item } = event;if (item.layer.type !== "group") {item.panel = {content: "legend",};}};// Get a reference to the arcgis-map elementconst viewElement = document.querySelector("arcgis-map");// Wait for the map component to be ready before accessing its properties.viewElement.addEventListener("arcgisViewReadyChange", () => {const { portalItem } = viewElement.map;}); -
Next, add a heading, description, and thumbnail to your app’s UI using the PortalItem. To do so, set Navigation Logo’s
heading,description, andthumbnailproperties to the PortalItem’stitle,snippetandthumbnailUrlproperties.main.js26 collapsed linesimport "./style.css";import "@arcgis/map-components/components/arcgis-layer-list";import "@arcgis/map-components/components/arcgis-map";import "@arcgis/map-components/components/arcgis-zoom";import "@esri/calcite-components/components/calcite-navigation";import "@esri/calcite-components/components/calcite-navigation-logo";import "@esri/calcite-components/components/calcite-shell";// Get a reference to the arcgis-layer-list elementconst arcgisLayerList = document.querySelector("arcgis-layer-list");// Set the listItemCreatedFunction to add a legend to each list itemarcgisLayerList.listItemCreatedFunction = (event) => {const { item } = event;if (item.layer.type !== "group") {item.panel = {content: "legend",};}};// Get a reference to the arcgis-map elementconst viewElement = document.querySelector("arcgis-map");// Wait for the map component to be ready before accessing its properties.viewElement.addEventListener("arcgisViewReadyChange", () => {const { portalItem } = viewElement.map;const navigationLogo = document.querySelector("calcite-navigation-logo");navigationLogo.heading = portalItem.title;navigationLogo.description = portalItem.snippet;navigationLogo.thumbnail = portalItem.thumbnailUrl;}); -
Find the accidental deaths layer in the
view.map.layerscollection.main.js36 collapsed linesimport "./style.css";import "@arcgis/map-components/components/arcgis-layer-list";import "@arcgis/map-components/components/arcgis-map";import "@arcgis/map-components/components/arcgis-zoom";import "@esri/calcite-components/components/calcite-navigation";import "@esri/calcite-components/components/calcite-navigation-logo";import "@esri/calcite-components/components/calcite-shell";// Get a reference to the arcgis-layer-list elementconst arcgisLayerList = document.querySelector("arcgis-layer-list");// Set the listItemCreatedFunction to add a legend to each list itemarcgisLayerList.listItemCreatedFunction = (event) => {const { item } = event;if (item.layer.type !== "group") {item.panel = {content: "legend",};}};// Get a reference to the arcgis-map elementconst viewElement = document.querySelector("arcgis-map");// Wait for the map component to be ready before accessing its properties.viewElement.addEventListener("arcgisViewReadyChange", () => {const { portalItem } = viewElement.map;const navigationLogo = document.querySelector("calcite-navigation-logo");navigationLogo.heading = portalItem.title;navigationLogo.description = portalItem.snippet;navigationLogo.thumbnail = portalItem.thumbnailUrl;const layer = viewElement.map.layers.find((layer) => layer.id === "Accidental_Deaths_8938");1 collapsed line}); -
Modify the layer’s PopupTemplate title.
main.js36 collapsed linesimport "./style.css";import "@arcgis/map-components/components/arcgis-layer-list";import "@arcgis/map-components/components/arcgis-map";import "@arcgis/map-components/components/arcgis-zoom";import "@esri/calcite-components/components/calcite-navigation";import "@esri/calcite-components/components/calcite-navigation-logo";import "@esri/calcite-components/components/calcite-shell";// Get a reference to the arcgis-layer-list elementconst arcgisLayerList = document.querySelector("arcgis-layer-list");// Set the listItemCreatedFunction to add a legend to each list itemarcgisLayerList.listItemCreatedFunction = (event) => {const { item } = event;if (item.layer.type !== "group") {item.panel = {content: "legend",};}};// Get a reference to the arcgis-map elementconst viewElement = document.querySelector("arcgis-map");// Wait for the map component to be ready before accessing its properties.viewElement.addEventListener("arcgisViewReadyChange", () => {const { portalItem } = viewElement.map;const navigationLogo = document.querySelector("calcite-navigation-logo");navigationLogo.heading = portalItem.title;navigationLogo.description = portalItem.snippet;navigationLogo.thumbnail = portalItem.thumbnailUrl;const layer = viewElement.map.layers.find((layer) => layer.id === "Accidental_Deaths_8938");layer.popupTemplate.title = "Accidental Deaths";2 collapsed lines});
Run the application
- Start the application from your project directory by running npm run dev in a terminal window. It should launch and be visible in your browser at http://localhost:5173.