This sample demonstrates how to use a MediaLayer in a 2D Map to display static images. The MediaLayer allows you to position images on the map by defining their geographic locations and configuring their extent and rotation.
How it works
A state
object is initialized with an empty array to store image elements and a new MediaLayer instance
const state = {
imageElements: [],
mediaLayer: new MediaLayer({
title: "New Orleans",
}),
};
The sample features old topographic maps of New Orleans from the 1890s. Details about each map, such as its name, title, and geographic extent, are stored in an image
array. Image elements are dynamically created and added to the state by iterating over this array and invoking the create
function on each image.
// A function to create an image element.
// The function takes the name of the image and its bounding box coordinates as parameters.
// It returns a new ImageElement instance with the specified image and georeference.
function createImageElement(name, extent) {
const imageElement = new ImageElement({
image: `https://arcgis.github.io/arcgis-samples-javascript/sample-data/media-layer/new-orleans/${name}.png`,
georeference: new ExtentAndRotationGeoreference({
extent: new Extent({
spatialReference: {
wkid: 102100,
},
xmin: extent.xmin,
ymin: extent.ymin,
xmax: extent.xmax,
ymax: extent.ymax,
}),
}),
});
return imageElement;
}
A Calcite List is created with a list item for each image element, allowing users to select and adjust the opacity of individual image elements.
// A function to create a list item for each image.
// The function takes the image information and index as parameters.
// It creates a list item with a label and a slider for adjusting the opacity of the image.
function createListItem(imageInfo, index) {
const listItem = document.createElement("calcite-list-item");
listItem.label = imageInfo.title;
listItem.dataset.imageName = imageInfo.name;
listItem.selected = true;
const sliderDiv = document.createElement("div");
sliderDiv.slot = "content-bottom";
const sliderLabel = document.createElement("calcite-label");
sliderLabel.layout = "inline";
sliderLabel.innerText = "Opacity";
const slider = document.createElement("calcite-slider");
slider.value = 100;
slider.addEventListener("calciteSliderInput", (event) => {
const value = Number(event.target.value ?? 0);
state.imageElements[index].opacity = value / 100;
});
sliderLabel.appendChild(slider);
sliderDiv.appendChild(sliderLabel);
listItem.appendChild(sliderDiv);
list?.appendChild(listItem);
}
Users can add or remove image elements from the MediaLayer's source by selecting or deselecting items in the list.
// A function to update the layer source with the selected images.
// The function retrieves the selected items from the list and updates the source of the media layer
// with the corresponding image elements from the imageElements array.
async function updateLayer() {
// Create a Set of selected image names for faster lookup
const selectedImageNames = new Set(
list.selectedItems.map((item) => item.dataset.imageName),
);
const source = state.mediaLayer.source;
source.elements.removeAll();
// Filter and add matching image elements to the source
state.imageElements.forEach((element) => {
const imageName = element.image?.split("/").pop().replace(".png", "");
if (selectedImageNames.has(imageName)) {
source.elements.push(element);
}
});
}
The luminosity blend mode can be applied with a calcite switch at the bottom of the list
// Change the blend mode of the media layer when the switch is toggled.
document.querySelector("#luminosity-switch")?.addEventListener("calciteSwitchChange", () => {
state.mediaLayer.blendMode =
state.mediaLayer.blendMode === "normal" ? "luminosity" : "normal";
});