Use the MapLibre ArcGIS plugin to query and display features (cities) with a population greater than 250,000.
Prerequisites
- You need an ArcGIS Location Platform or ArcGIS Online account.
- You need to complete Step 2. Create an API key.
Steps
Get the service URL
To display the feature layer in an app, reference its service URL.
-
In your portal, go to the item page
An item page is a web page in ArcGIS Online or the developer dashboard used to access and manage the properties for an item and the content it references such as a web map, hosted layer, or file. of your feature service. -
Scroll down to the URL section and copy the value. You will need this in a later step. It should look something like this:
https://services3.arcgis.com/ G Vgb Jbqm8h XASV Yi/arcgis/rest/services/ USA _Major _Cities/ Feature Server
Create a new app
Create a new CodePen app with a map div that is the full width and height of the browser window.
-
Go to CodePen and create a new pen for your application.
-
In CodePen > HTML, add HTML and CSS to create a page with a div element called
map.Use dark colors for code blocks <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no"> <title>MapLibre ArcGIS</title> <style> html, body, #map { padding: 0; margin: 0; height: 100%; width: 100%; font-family: Arial, Helvetica, sans-serif; font-size: 14px; color: #323232; } </style> </head> <body> <div id="map"></div> </body> </html>
Add script references
Reference the MapLibre GL JS library and the MapLibre ArcGIS plugin.
-
In the HTML
<head, add the following> <linkand> <scriptreferences.> Use dark colors for code blocks <!-- Load MapLibre GL JS from CDN --> <script src="https://unpkg.com/maplibre-gl@5.23.0/dist/maplibre-gl.js"></script> <link href="https://unpkg.com/maplibre-gl@5.23.0/dist/maplibre-gl.css" rel="stylesheet"> <!-- Load MapLibre ArcGIS from CDN --> <script src="https://unpkg.com/@esri/maplibre-arcgis@1.1.0/dist/umd/maplibre-arcgis.min.js"></script>
Set the access token
-
Add a
<scriptelement in the HTML> <bodyand create an> accessvariable to store the access tokenToken An access token is an authorization string that provides access to secure ArcGIS content, data, and services. Its capabilities are determined by the privileges it supports. It is obtained by implementing API key authentication, User authentication, or App authentication. . SetYOURwith the access token you previously copied._ACCESS _TOKEN Use dark colors for code blocks <script> /* Use for API key authentication */ const accessToken = "YOUR_ACCESS_TOKEN"; </script>
Create a map
Create a map to display an ArcGIS basemap style centered on the United States of America. Use the MapLibre ArcGIS plugin to access the basemap style.
-
Create a
Mapthat centers on the United States of America.Use dark colors for code blocks const map = new maplibregl.Map({ container: "map", // the id of the div element zoom: 3, center: [-100, 40] }); -
Use the plugin to apply a
Basemapthat references theStyle arcgis/navigationstyle.Use dark colors for code blocks const basemapStyle = maplibreArcGIS.BasemapStyle.applyStyle(map, { style: "arcgis/navigation", token: accessToken });
Add a feature layer
Use the MapLibre ArcGIS plugin to add a feature layer to the map that displays the GeoJSON features. You can use a query (such as a where clause) to return a set of features, for example only cities with populations greater than 250,000. This helps reduce data transfer and helps your application load faster.
-
Add a
map.on('load', ...)to register an event handler that runs only once when the map has finished loading.Use dark colors for code blocks map.once("load", async () => { map.addSource(featureLayer.sourceId, { ...featureLayer.source, cluster: true, clusterRadius: 20, // cluster two trailheads if less than 20 pixels apart clusterMaxZoom: 14 // display all trailheads individually from zoom 14 up }); }); -
Inside the load event handler, use the plugin to create a new
Featureobject that references the feature service URL you copied earlier. Configure theLayer queryso it only returns cities with a population greater than 250,000.Use dark colors for code blocks map.once("load", async () => { // Get GeoJSON features from a feature service where `POPULATION > 250000` const featureLayer = await maplibreArcGIS.FeatureLayer.fromUrl( "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/USA_Major_Cities/FeatureServer/0", { query: { where: "POPULATION>250000" }, token: accessToken }); }); -
Add the feature layer source to the map. Add
cluster,clusterandRadius clusterattributes to the definition of theMax Zoom trailheadssource to enable feature clustering.Use dark colors for code blocks map.once("load", async () => { // Get GeoJSON features from a feature service where `POPULATION > 250000` const featureLayer = await maplibreArcGIS.FeatureLayer.fromUrl( "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/USA_Major_Cities/FeatureServer/0", { query: { where: "POPULATION>250000" }, token: accessToken }); map.addSource(featureLayer.sourceId, { ...featureLayer.source, cluster: true, clusterRadius: 20, // cluster two trailheads if less than 20 pixels apart clusterMaxZoom: 14 // display all trailheads individually from zoom 14 up }); });
Add a circle layer
Add a circle layer to visualize clustered features from the GeoJSON source.
-
Call
map.addto create a layer withLayer() typethat references the: "circle" usasource. Use theMajor Cities paintproperty to style the layer based on whether a feature is part of a cluster.Use dark colors for code blocks map.once("load", async () => { // Get GeoJSON features from a feature service where `POPULATION > 250000` const featureLayer = await maplibreArcGIS.FeatureLayer.fromUrl( "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/USA_Major_Cities/FeatureServer/0", { query: { where: "POPULATION>250000" }, token: accessToken }); map.addSource(featureLayer.sourceId, { ...featureLayer.source, cluster: true, clusterRadius: 20, // cluster two trailheads if less than 20 pixels apart clusterMaxZoom: 14 // display all trailheads individually from zoom 14 up }); map.addLayer({ ...featureLayer.layer, id: "city-clusters", type: "circle", paint: { "circle-color": "hsla(0,0%,0%,0.75)", "circle-stroke-width": 1.5, "circle-stroke-color": "white", "circle-radius": ["case", ["get", "cluster"], 10, 5] // 10 pixels for clusters, 5 pixels otherwise } }); });
Cluster points
Add a symbol layer to display cluster counts on top of circle clusters.
- Create a symbol layer named
cities-cluster-countto display the number of features in each cluster. This layer references theusasource and uses theMajor Cities pointproperty for the label._count Use dark colors for code blocks map.once("load", async () => { // Get GeoJSON features from a feature service where `POPULATION > 250000` const featureLayer = await maplibreArcGIS.FeatureLayer.fromUrl( "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/USA_Major_Cities/FeatureServer/0", { query: { where: "POPULATION>250000" }, token: accessToken }); map.addSource(featureLayer.sourceId, { ...featureLayer.source, cluster: true, clusterRadius: 20, // cluster two trailheads if less than 20 pixels apart clusterMaxZoom: 14 // display all trailheads individually from zoom 14 up }); map.addLayer({ ...featureLayer.layer, id: "city-clusters", type: "circle", paint: { "circle-color": "hsla(0,0%,0%,0.75)", "circle-stroke-width": 1.5, "circle-stroke-color": "white", "circle-radius": ["case", ["get", "cluster"], 10, 5] // 10 pixels for clusters, 5 pixels otherwise } }); map.addLayer({ ...featureLayer.layer, id: "cities-cluster-count", type: "symbol", layout: { "text-font": ["Arial Bold"], "text-field": ["get", "point_count"], "text-offset": [0, 0.1] // move the label vertically downwards slightly to improve centering }, paint: { "text-color": "white" } }); });
Run the app
Run the app.
The map should display the styled feature layer from the GeoJSON data hosted in your portal.