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 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> <head> <meta charset="utf-8" /> <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no" /> <title>MapLibre GL JS</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> <link
and> <script
references.> Use dark colors for code blocks <!-- Load MapLibre GL JS from CDN --> <script src=https://unpkg.com/maplibre-gl@5.9.0/dist/maplibre-gl.js></script> <link href=https://unpkg.com/maplibre-gl@5.9.0/dist/maplibre-gl.css rel="stylesheet" /> <!-- Load MapLibre ArcGIS from CDN --> <script src="https://unpkg.com/@esri/maplibre-arcgis@1.0.0/dist/umd/maplibre-arcgis.min.js"></script>
Set the access token
-
Add a
<script
element in the HTML> <body
and create an> access
variable to store the access token. SetToken YOUR
with 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
Map
that 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
Basemap
that references theStyle arcgis/navigation
style.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
Feature
object that references the feature service URL you copied earlier. Configure theLayer query
so 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
,cluster
andRadius cluster
attributes to the definition of theMax Zoom trailheads
source 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.add
to create a layer withLayer() type
that references the: "circle" usa
source. Use theMajor Cities paint
property 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-count
to display the number of features in each cluster. This layer references theusa
source and uses theMajor Cities point
property 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.