Learn how to display feature
A popup
In this tutorial, you use the MapLibre ArcGIS plugin to add a feature layer. Then, you create an interactive popup. When a feature
Prerequisites
You need an ArcGIS Location Platform or ArcGIS Online account.
Steps
Get the starter app
Select a type of authentication and follow the steps to create a new app.
Choose API key authentication if you:
- Want the easiest way to get started.
- Want to build public applications
A public application is an application that allows anonymous access without requiring users to sign in with an ArcGIS account. It supports API key or app authentication. that access ArcGIS Location ServicesArcGIS Location Services, also referred to as Location Services, are services hosted by Esri that provide geospatial functionality for developing mapping applications. They include the ArcGIS Basemap Styles service, ArcGIS Static Basemap Tiles service, ArcGIS Places service, ArcGIS Geocoding service, ArcGIS Routing service, ArcGIS GeoEnrichment service, and ArcGIS Elevation service. An ArcGIS Location Platform or ArcGIS Online account is required to use the services. and secure itemsAn item, also known as a content item, is a resource stored in a portal such as a web map, hosted layer, style, script tool, file, or notebook. . - Have an ArcGIS Location Platform or ArcGIS Online account.
Choose user authentication if you:
- Want to build private applications.
- Require application users to sign in with their own ArcGIS account
An ArcGIS account is an identity with a user type and set of privileges that can access specific ArcGIS products, tools, APIs, services, and resources. The main account types that can be used for development are an ArcGIS Location Platform account, ArcGIS Online account, and ArcGIS Enterprise account. ArcGIS Location Platform and ArcGIS Online accounts are also associated with a subscription. and access resources their behalf. - Have an ArcGIS Online account.
To learn more about both types of authentication, go to Authentication.
Set up authentication
Set developer credentials
Use the API key or OAuth developer credentials
Add a load handler
You need to wait for the map to be completely loaded before adding any layers.
-
Add an event handler to the map
loadevent.Use dark colors for code blocks <script> /* Use for API key authentication */ const accessToken = "YOUR_ACCESS_TOKEN"; // or /* Use for user authentication */ // const session = await arcgisRest.ArcGISIdentityManager.beginOAuth2({ // clientId: "YOUR_CLIENT_ID", // Your client ID from OAuth credentials // redirectUri: "YOUR_REDIRECT_URI", // The redirect URL registered in your OAuth credentials // portal: "YOUR_PORTAL_URL" // Your portal URL // }) // const accessToken = session.token; const map = new maplibregl.Map({ container: "map", // the id of the div element zoom: 12, // starting zoom center: [-118.805, 34.027] // starting location [longitude, latitude] }); const basemapStyle = maplibreArcGIS.BasemapStyle.applyStyle(map, { style: "arcgis/outdoor", token: accessToken }); map.once("load", async () => { }); </script>
Add the trailheads layer
You will use a feature layer and a circle layer
-
Inside the load handler, use the plugin to create a new
Featureobject that references the Trailheads feature service URL.Layer Use dark colors for code blocks map.once("load", async () => { const featureLayer = await maplibreArcGIS.FeatureLayer.fromPortalItem("883cedb8c9fe4524b64d47666ed234a7"); featureLayer.addSourcesTo(map); }); -
Add a
circlelayer calledtrailheads-circle.Use dark colors for code blocks map.once("load", async () => { const featureLayer = await maplibreArcGIS.FeatureLayer.fromPortalItem("883cedb8c9fe4524b64d47666ed234a7"); featureLayer.addSourcesTo(map); map.addLayer({ ...featureLayer.layer, id: "trailheads-circle", type: "circle", paint: { "circle-color": "hsla(200,80%,70%,0.5)", "circle-stroke-width": 2, "circle-radius": 5, "circle-stroke-color": "hsl(200,80%,50%)" } }); });
Add a click handler
To display a popup appear when a trailheads feature is clicked, add a click event handler. This handler is called with the features under the pointer where you clicked.
-
Add a handler for the
clickevent on theMap. Pass thetrailheads-circleid to theonmethod so the handler is only called when you click on that layer. Inside, store the first element of thefeaturesproperty to atrailheadvariable.If there are multiple overlapping features where you click, all of those features will be included in the
featuresarray. The first element listed will be the topmost feature.See the MapLibre GL JS documentation for more map events you can respond to.
Use dark colors for code blocks paint: { "circle-color": "hsla(200,80%,70%,0.5)", "circle-stroke-width": 2, "circle-radius": 5, "circle-stroke-color": "hsl(200,80%,50%)" } }); map.on("click", "trailheads-circle", (e) => { const trailhead = e.features[0]; });
Create the popup
You create a new Popup with mapboxgl.. The default parameters give a simple white bubble which stays open until you click its close button or somewhere on the map.
To add content, you use Popup.set. Use your trailhead variable to make the HTML string. It is a GeoJSONproperties object. The name of the trail is stored in TRL and the name of the park service is in PARK.
-
Create a new
Popup.You can pass additional options, to change behavior such as whether there is a close button in the corner. See the MapLibre documentation for more details.
Use dark colors for code blocks map.on("click", "trailheads-circle", (e) => { const trailhead = e.features[0]; new maplibregl.Popup() }); -
Use
Popup.setto set the contents: the name of the trail in anHTML <h3tag, then the name of the park service.> Use dark colors for code blocks new maplibregl.Popup() .setHTML(`<b>${trailhead.properties.TRL_NAME}</b><br>${trailhead.properties.PARK_NAME}`)
Add the popup to the map
When you create the Popup, it is not immediately added to the map. You need to call set to provide the position, then add to attach it to the Map.
-
Set the location of the
Popupto the location of the feature clicked on by using thesetmethod, then add it to the map.Lng Lat Use dark colors for code blocks new maplibregl.Popup() .setHTML(`<b>${trailhead.properties.TRL_NAME}</b><br>${trailhead.properties.PARK_NAME}`) .setLngLat(trailhead.geometry.coordinates) .addTo(map);
Change the cursor on hover
To indicate that you can interact with a layer by clicking, it is useful to change the mouse cursor to a pointing hand when hovering over the layer. You use Map.get to access the map's <canvas element, so you can set the CSS cursor property.
-
Add a handler to the
mouseenterevent, changing the cursor to a pointer.Use dark colors for code blocks map.on("click", "trailheads-circle", (e) => { const trailhead = e.features[0]; new maplibregl.Popup() .setHTML(`<b>${trailhead.properties.TRL_NAME}</b><br>${trailhead.properties.PARK_NAME}`) .setLngLat(trailhead.geometry.coordinates) .addTo(map); }); map.on("mouseenter", "trailheads-circle", () => { map.getCanvas().style.cursor = "pointer"; }); -
Add a handler to the
mouseleaveevent, changing the cursor back to the default.Use dark colors for code blocks map.on("mouseenter", "trailheads-circle", () => { map.getCanvas().style.cursor = "pointer"; }); map.on("mouseleave", "trailheads-circle", () => { map.getCanvas().style.cursor = ""; });
Run the app
Run the app.
The map view should display the Trailheads feature layerWhat's next?
Learn how to use additional location services in these tutorials:

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.

Get local data
Query regional facts and spending trends for a study area in the United States.

Add a feature layer as GeoJSON
Display and style GeoJSON features from a feature service.