Add interactivity to your map by showing a city’s population count in a popup.
Prerequisites
- You need an ArcGIS Location Platform or ArcGIS Online account.
- You need to complete Step 3. Style GeoJSON data.
Steps
Add mouse event handlers
-
Add a
mouseenterevent handler on thecity-clusterslayer. Change the cursor style to a pointer and ensure that the hovered feature is not a cluster.Use dark colors for code blocks map.on("mouseenter", "city-clusters", (e) => { map.getCanvas().style.cursor = "pointer"; if (e.features[0].properties.cluster) return; }); -
Create variables for the population value and feature coordinates. Format the
POPULATIONproperty for readability and extract the coordinates fromgeometry.coordinates.Use dark colors for code blocks map.on("mouseenter", "city-clusters", (e) => { map.getCanvas().style.cursor = "pointer"; if (e.features[0].properties.cluster) return; const population = new Intl.NumberFormat().format(e.features[0].properties.POPULATION); const coordinates = e.features[0].geometry.coordinates.slice(); }); -
Add a
mouseleaveevent handler on thecity-clusterslayer. Reset the cursor style back to default.Use dark colors for code blocks map.on("mouseleave", "city-clusters", () => { map.getCanvas().style.cursor = ""; });
Add a popup
-
Create a
Popup()object.Use dark colors for code blocks const popup = new maplibregl.Popup({ closeButton: false, closeOnClick: false }); map.on("mouseenter", "city-clusters", (e) => { map.getCanvas().style.cursor = "pointer"; if (e.features[0].properties.cluster) return; const population = new Intl.NumberFormat().format(e.features[0].properties.POPULATION); const coordinates = e.features[0].geometry.coordinates.slice(); }); map.on("mouseleave", "city-clusters", () => { map.getCanvas().style.cursor = ""; }); -
Inside the
mouseenterevent, set the popup's position usingset, define the content to show the city's population count, and add it to the map.Lng Lat Use dark colors for code blocks const popup = new maplibregl.Popup({ closeButton: false, closeOnClick: false }); map.on("mouseenter", "city-clusters", (e) => { map.getCanvas().style.cursor = "pointer"; if (e.features[0].properties.cluster) return; const population = new Intl.NumberFormat().format(e.features[0].properties.POPULATION); const coordinates = e.features[0].geometry.coordinates.slice(); popup.setLngLat(coordinates).setHTML(`<b>Population</b>: ${population}`).addTo(map); }); map.on("mouseleave", "city-clusters", () => { map.getCanvas().style.cursor = ""; }); -
Inside the
mouseleaveevent, remove the popup from the map so it doesn’t stay visible after leaving the feature.Use dark colors for code blocks const popup = new maplibregl.Popup({ closeButton: false, closeOnClick: false }); map.on("mouseenter", "city-clusters", (e) => { map.getCanvas().style.cursor = "pointer"; if (e.features[0].properties.cluster) return; const population = new Intl.NumberFormat().format(e.features[0].properties.POPULATION); const coordinates = e.features[0].geometry.coordinates.slice(); popup.setLngLat(coordinates).setHTML(`<b>Population</b>: ${population}`).addTo(map); }); map.on("mouseleave", "city-clusters", () => { map.getCanvas().style.cursor = ""; popup.remove(); });
Run the app
Run the app.
When you hover over a city, a popup should appear with the city's population.Congratulations!
You have completed the Create and style features how-to. The application displays clustered U.S. cities with populations over 250,000. It allows you to zoom in on a cluster and hover over it to see its population.
What's next?

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

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

Get local data
Query regional facts and spending trends for a study area in the United States.
To explore additional ArcGIS capabilities, go to Tutorials.