This sample demonstrates how to create and add a CatalogLayer to a Map component. The CatalogLayer points to different portal items and services, helping you to organize and manage your data. It also makes it simpler for users to find information. Instead of manually gathering and adding each dataset separately in your map, you can create a CatalogLayer, a centralized reference point for all the data you need.
The map shows historic Sanborn fire insurance maps for different years for Cleveland, OH, Pittsburgh, PA and Toronto, Canada. The catalog layer also references the latest neighborhood and parcel feature services for the same areas.
The catalog layer is initialized as shown below when the application loads:
// Create a new CatalogLayer instance from a portal item const layer = new CatalogLayer({ portalItem: { id: "d457418d6d374bd08b899dccf4289bfd", }, outFields: ["*"], useViewTime: false, });This time-enabled catalog layer can be used with the Time Bar component. Time filtering is initially disabled. Users can enable or disable it with the switch, which sets the CatalogLayer’s useViewTime property and enables or disables the Time Bar component. When enabled, the layer uses the map’s time extent to filter its data.
// enable or disable the time bar based on the time button state const timeButton = document.getElementById("time-button"); timeButton.addEventListener("calciteSwitchChange", () => { const isChecked = timeButton.checked; layer.useViewTime = isChecked; timeBar.disabled = !isChecked; });CatalogLayer references different types of services including but not limited to feature services, map services, scene services and image services. This catalog layer references feature and map services. Users can apply a featureEffect to the CatalogLayer’s footprintLayer based on the selected item type, as shown below. Other item types are de-emphasized with grayscale and reduced opacity. Selecting “Show all” removes the effect.
// Filter the catalog layer by selected item type const itemTypeChip = document.getElementById("item-type-chips"); itemTypeChip.addEventListener("calciteChipGroupSelect", async (event) => { await viewElement.goTo(layer.fullExtent); const selected = event.target.selectedItems?.[0]; if (!selected) return; const itemType = selected.value;
// Set the feature effect to emphasize the selected item type layer.footprintLayer.featureEffect = itemType === "Show all" ? undefined : { filter: { where: `cd_itemType = '${itemType}'` }, excludedEffect: "grayscale(70%) opacity(50%)", }; });Once users find the layers they need in the map, they can save the catalog layer’s dynamically loaded layers to a new GroupLayer portal item, as shown below. The application opens the saved item’s page in a new tab.
const groupLayerTitleInput = document.getElementById("grouplayer-title-input"); document.getElementById("save-grouplayer-button").addEventListener("click", async () => { try { // Get all visible layers from the dynamicGroupLayer const layers = await Promise.all( layer.dynamicGroupLayer.layers.map((layer) => createLayerFromDynamicLayer(layer)), );
// If no layers are visible, show a message and exit if (!layers.length) { alert("No visible layers to save in the group layer."); return; }
// Create a new group layer and add the visible sub layers of the dynamicGroupLayer const groupLayer = new GroupLayer({ title: groupLayerTitleInput.value, layers, });
await groupLayer.loadAll();
// Save the group layer as a portal item const portalItem = new PortalItem({ title: groupLayerTitleInput.value, }); const savedItem = await groupLayer.saveAs(portalItem);
// Open the portal item page in a new tab window.open(savedItem.itemPageUrl, "_blank"); } catch (error) { if (error.name !== "identity-manager:user-aborted") { alert("Failed to save group layer: " + error.message); console.error(error); } } });