Display a pop-up
Learn how to display feature attributes in a pop-up.
A pop-up, also known as a "popup", is a visual element that displays information about a feature when it is clicked. You typically style and configure a pop-up using HTML and CSS for each layer in a map. Pop-ups can display attribute values, calculated values, or rich content such as images, charts, or videos.
In this tutorial, you create an interactive pop-up for the Trailheads feature layer in the Santa Monica Mountains. When a feature is clicked, a pop-up is displayed containing the name of the trail and the service that manages it. You use the ol-popup library to achieve this.
Prerequisites
You need an ArcGIS Developer or ArcGIS Online account to access the developer dashboard and create an API key.
Steps
Create a new pen
- To get started, either complete the Display a map tutorial or .
Set the API key
To access location services, you need an API key or OAuth 2.0 access token. To learn how to create and scope your key, visit the Create an API key tutorial.
Go to your dashboard to get an API key. The API key must be scoped to access the services used in this tutorial.
In CodePen, update
api
to use your key.Key Use dark colors for code blocks Change line
Add the pop-up library
OpenLayers does not include a pop-up component. You can use the third-party library ol-popup to simplify adding pop-ups to your map.
In the
<head>
element, add references to the ol-popup library.Use dark colors for code blocks Add line. Add line.
Add the trailheads layer
Use a Vector
layer with a Vector
source to display the trailheads as GeoJSON.
Before the
olms
initialization, create a aVector
layer. Give it aVector
source with a GeoJSON feature format to load and display the trailheads feature layer. Save it to atrailheads
variable.Layer Use dark colors for code blocks Add line. Add line. Add line. Add line. Add line. Add line. Add a load handler to the
olms
initialization. Inside, Usemap.add
to add this layer to the map.Layer Use dark colors for code blocks Add line. Add line. Add line.
Initialize the pop-up
A Popup
is a type of Overlay
. Use map.add
to add it to the map.
Create a new
Popup
and save it to apopup
variable. Add it to the map withmap.add
.Overlay Use dark colors for code blocks Add line. Add line.
Display a pop-up on click
A pop-up can display any HTML content. Use a click
event handler to detect the click, then use map.get
to find any trailhead features at the clicked point. You pass a function as a layer
to do so.
If the returned array contains at least one feature, you can use Feature.get
to extract the trail name (TRL_
) and park name (PARK_
). You then use popup.show
to update the pop-up position and content.
Add a
click
event handler. Inside, useget
to get an array of trailhead features clicked on.Features At Pixel Use dark colors for code blocks Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. If there is a trail name to show, use
popup.show
to set the position and content of the pop-up. Otherwise, usepopup.hide
to hide it.Use dark colors for code blocks Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line.
Run the app
In CodePen, run your code to display the map.
The map view should display the Trailheads feature layer. When you click a feature, the name of the trailhead and its park name is displayed in a pop-up.
What's next?
SharedTutorialContent WhatsNext />

Query a feature layer (spatial)
Execute a spatial query to access polygon features from a feature service.

Get global data
Query demographic information for locations around the world with the GeoEnrichment service.

Get local data
Query regional facts, spending trends, and psychographics with the GeoEnrichment service.

Add a feature layer
Add features from feature layers to a map.