Skip to content

Intro to popup component

Popup components provide an easy way to add interactivity to your ArcGIS Maps SDK for JavaScript applications by displaying information in response to a user action. This sample demonstrates how to use the Popup component in a Map component to display a popup at a clicked location in the map. While the popup is typically used in conjunction with layers, you can also display the popup in response to a query or some other action that does not involve a graphic or a feature. In this example, the popup displays the latitude and longitude of the clicked location in the heading and the address of the clicked location in the content with the help of a locator.

Configure the Map and Popup components

Create a Map component and set the basemap, center, and zoom attributes. The map component currently uses the Popup widget by default. To utilize the Popup component instead, add an <arcgis-popup> as a child of the Map component in the "popup" slot. Since the Popup component is not tied to any layers in this example, set the popup-disabled attribute on the Map component to disable the default popup behavior.

Use dark colors for code blocksCopy
1
2
3
<arcgis-map basemap="streets-navigation-vector" zoom="12" center="-71.6899, 43.7598" popup-disabled>
  <arcgis-popup slot="popup"></arcgis-popup>
</arcgis-map>

Listen to the map's click event and display the popup at the clicked location

To display the popup component with location information, add an event listener to the map component's arcgisViewClick event and get the longitude and latitude of the clicked location. Then set the location and the title properties within the the open() method to open the popup.

Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
viewElement.addEventListener("arcgisViewClick", (event) => {
  const {
    mapPoint
  } = event.detail;
  const lat = Math.round(mapPoint.latitude * 1000) / 1000;
  const lon = Math.round(mapPoint.longitude * 1000) / 1000;
  popupComponent.open({
    // Set the popup's title to the coordinates of the location
    title: `Reverse geocode: [${lon}, ${lat}]`,
    location: mapPoint, // Set the location of the popup to the clicked location
  });
  const params = {
    location: mapPoint,
  };
  // Execute a reverse geocode using the clicked location
  locator
    .locationToAddress(locatorUrl, params)
    .then((response) => {
      // If an address is successfully found, show it in the popup's content
      popupComponent.content = response.address;
    })
    .catch(() => {
      // If the promise fails and no result is found, show a generic message
      popupComponent.content = "No address was found for this location";
    });
});
Image preview of related sample Intro to PopupTemplate

Intro to PopupTemplate

Image preview of related sample Multiple popup elements

Multiple popup elements

Image preview of related sample Reference Arcade expressions in PopupTemplate

Reference Arcade expressions in PopupTemplate

Image preview of related sample Summarize intersecting points in a popup

Summarize intersecting points in a popup

Image preview of related sample Custom popup content

Custom popup content

Image preview of related sample Popup dock positions

Popup dock positions

Image preview of related sample Popup with edit action

Popup with edit action

Image preview of related sample Custom popup actions per feature attribute

Custom popup actions per feature attribute

Image preview of related sample Popup actions

Popup actions

Image preview of related sample Browse related records in a popup

Browse related records in a popup

Image preview of related sample PopupTemplate - use functions to set content

PopupTemplate - use functions to set content

Image preview of related sample PopupTemplate with promise

PopupTemplate with promise

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.