Learn how to add features from feature layers to a map.
A feature layer is a dataset in a hosted feature service. Each feature layer contains features with a single geometry type (point, line, or polygon), and a set of attributes. You can access and display features by making query requests to the feature service and displaying them in a map.
In this tutorial, you access and display three different hosted feature layers. The data is accessed and displayed as GeoJSON.
The feature layers are:
Prerequisites
You need an ArcGIS Location Platform or ArcGIS Online account.
Steps
Create a new pen
- To get started, you can complete the Display a map tutorial or use the .
Get an access token
You need an access token with the correct privileges to access the resources used in this tutorial.
-
Go to the Create an API key tutorial and create an API key with the following privilege(s):
- Privileges
- Location services > Basemaps
- Item access
- Note: If you are using your own custom data layer for this tutorial, you need to grant the API key credentials access to the layer item. Learn more in Item access privileges.
- Privileges
-
Copy the API key access token to your clipboard when prompted.
-
In CodePen, update the
access
variable to use your access token.Token Use dark colors for code blocks const accessToken = "YOUR_ACCESS_TOKEN"; const basemapId = "arcgis/outdoor"; const basemapURL = `https://basemapstyles-api.arcgis.com/arcgis/rest/services/styles/v2/styles/${basemapId}?token=${accessToken}`; olms.apply(map, basemapURL);
-
Run the code to ensure the basemap is displayed in the map.
To learn about the other types of authentication available, go to Types of authentication.
Add references to ArcGIS REST JS
This tutorial uses ArcGIS REST JS for accessing the feature service. It also uses the ol-popup library to display pop-ups.
-
In the
<head
element, add references to the ArcGIS REST JS and ol-popup libraries.ss> Use dark colors for code blocks <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/ol@v10.1.0/ol.css" type="text/css" /> <script src="https://cdn.jsdelivr.net/npm/ol@v10.1.0/dist/ol.js"></script> <script src="https://cdn.jsdelivr.net/npm/ol-mapbox-style@12.3.5/dist/olms.js" type="text/javascript"></script> <script src="https://unpkg.com/@esri/arcgis-rest-request@4.0.0/dist/bundled/request.umd.js"></script> <script src="https://unpkg.com/ol-popup@5.1.1/dist/ol-popup.js"></script> <link rel="stylesheet" href="https://unpkg.com/ol-popup@5.1.1/src/ol-popup.css" />
Add a point feature layer
The trailheads feature layer contains feature with a point geometry. Use a Vector
layer to access the feature layer by URL and display the points. By default, they will display as white circles with blue outlines.
-
Create a
Vector
. Pass a newSource Geo
as theJSON format
parameter. For theurl
parameter, pass a direct link to a FeatureServer endpoint.The URL contains an
f=geojson
parameter to request data in GeoJSON format. For more information about querying a Feature Service, see the Query a feature layer (SQL) and Query a feature layer (spatial) tutorials.Use dark colors for code blocks const basemapId = "arcgis/navigation"; const basemapURL = `https://basemapstyles-api.arcgis.com/arcgis/rest/services/styles/v2/styles/${basemapId}?token=${accessToken}`; olms.apply(map, basemapURL).then(function (map) { const pointLayerName = "Trailheads"; const pointLayerURL = "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/" + pointLayerName + "/FeatureServer/0/query?where=1%3D1&outFields=*&returnGeometry=true&f=geojson"; const pointSource = new ol.source.Vector({ format: new ol.format.GeoJSON(), url: pointLayerURL, });
-
Add the data attribution for the feature layer source.
- Go to the Trailheads (Santa Monica Mountains) item.
- Scroll down to the Credits (Attribution) section and copy its value.
- Create an
attribution
property and paste the attribution value from the item.Use dark colors for code blocks const basemapId = "arcgis/navigation"; const basemapURL = `https://basemapstyles-api.arcgis.com/arcgis/rest/services/styles/v2/styles/${basemapId}?token=${accessToken}`; olms.apply(map, basemapURL).then(function (map) { const pointLayerName = "Trailheads"; const pointLayerURL = "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/" + pointLayerName + "/FeatureServer/0/query?where=1%3D1&outFields=*&returnGeometry=true&f=geojson"; const pointSource = new ol.source.Vector({ format: new ol.format.GeoJSON(), url: pointLayerURL, // Attribution text retrieved from https://arcgis.com/home/item.html?id=883cedb8c9fe4524b64d47666ed234a7 attributions: ['| Los Angeles GeoHub'] });
-
Create a
Vector
layer using theVector
source you defined. Add it to the map usingmap.add
.Layer Use dark colors for code blocks const pointSource = new ol.source.Vector({ format: new ol.format.GeoJSON(), url: pointLayerURL, // Attribution text retrieved from https://arcgis.com/home/item.html?id=883cedb8c9fe4524b64d47666ed234a7 attributions: ['| Los Angeles GeoHub'] }); const pointLayer = new ol.layer.Vector({ source: pointSource }); map.addLayer(pointLayer);
Add a line feature layer
The Trails feature layer contains features with a line geometry. Use a Vector
layer to access the feature layer by URL and display the lines. By default, the features are displayed as blue lines.
-
Create a
Vector
withSource format
andurl
parameters.Use dark colors for code blocks const pointLayer = new ol.layer.Vector({ source: pointSource }); map.addLayer(pointLayer); const lineLayerName = "Trails"; const lineLayerURL = "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/" + lineLayerName + "/FeatureServer/0/query?where=1%3D1&outFields=*&returnGeometry=true&f=geojson"; const lineSource = new ol.source.Vector({ format: new ol.format.GeoJSON(), url: lineLayerURL, });
-
Add the data attribution for the feature layer source.
- Go to the Trails item.
- Scroll down to the Credits (Attribution) section and copy its value.
- Create an
attribution
property and paste the attribution value from the item.Use dark colors for code blocks const pointLayer = new ol.layer.Vector({ source: pointSource }); map.addLayer(pointLayer); const lineLayerName = "Trails"; const lineLayerURL = "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/" + lineLayerName + "/FeatureServer/0/query?where=1%3D1&outFields=*&returnGeometry=true&f=geojson"; const lineSource = new ol.source.Vector({ format: new ol.format.GeoJSON(), url: lineLayerURL, // Attribution text retrieved from https://arcgis.com/home/item.html?id=69e12682738e467eb509d8b54dc73cbd attributions: ['| Los Angeles GeoHub'] });
-
Create a
Vector
layer using theVector
source you defined. Add it to the map usingmap.add
.Layer Use dark colors for code blocks const lineSource = new ol.source.Vector({ format: new ol.format.GeoJSON(), url: lineLayerURL, // Attribution text retrieved from https://arcgis.com/home/item.html?id=69e12682738e467eb509d8b54dc73cbd attributions: ['| Los Angeles GeoHub'] }); const lineLayer = new ol.layer.Vector({ source: lineSource }); map.addLayer(lineLayer);
Add a polygon feature layer
The Parks and Open Space feature layer contains features with a polygon geometry. Use a Vector
layer to access the feature layer by URL and display the polygons. By default, polygon features are displayed as translucent white fill with blue outlines.
If you added the layer with map.add
, it would be added above other layers and obscure them. Instead, use map.get
to access the layers collection, then use Collection.insert
to place it below the other two layers.
-
Create a
Vector
withSource format
andurl
parameters.Use dark colors for code blocks const lineLayer = new ol.layer.Vector({ source: lineSource }); map.addLayer(lineLayer); const polygonLayerName = "Parks_and_Open_Space"; const polygonLayerURL = "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/" + polygonLayerName + "/FeatureServer/0/query?where=1%3D1&outFields=*&returnGeometry=true&f=geojson"; const polygonSource = new ol.source.Vector({ format: new ol.format.GeoJSON(), url: polygonLayerURL, });
-
Add the data attribution for the feature layer source.
- Go to the Parks and Open Space item.
- Scroll down to the Credits (Attribution) section and copy its value.
- Create an
attribution
property and paste the attribution value from the item.Use dark colors for code blocks const lineLayer = new ol.layer.Vector({ source: lineSource }); map.addLayer(lineLayer); const polygonLayerName = "Parks_and_Open_Space"; const polygonLayerURL = "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/" + polygonLayerName + "/FeatureServer/0/query?where=1%3D1&outFields=*&returnGeometry=true&f=geojson"; const polygonSource = new ol.source.Vector({ format: new ol.format.GeoJSON(), url: polygonLayerURL, // Attribution text retrieved from https://arcgis.com/home/item.html?id=f2ea5d874dad427294641d2d45097c0e attributions: ['| Los Angeles GeoHub'] });
-
Create a
Vector
layer using theVector
source you defined. Add it to the map usinginsert
at index 1.At Use dark colors for code blocks const polygonSource = new ol.source.Vector({ format: new ol.format.GeoJSON(), url: polygonLayerURL, // Attribution text retrieved from https://arcgis.com/home/item.html?id=f2ea5d874dad427294641d2d45097c0e attributions: ['| Los Angeles GeoHub'] }); const polygonLayer = new ol.layer.Vector({ source: polygonSource }); map.getLayers().insertAt(1, polygonLayer);
-
In CodePen, click Run at the top right. Your map should display the locations of trailheads, trails, and open spaces on your map.
Add a pop-up
You can use a Popup
to show data attributes for a feature when you click on them. A popup is a type of Overlay
so you add it to the map with map.add
. To get a feature, you use the click event and map.get
to get the features at a click point. You also use the point to position the Popup
at the correct location.
For a full list of possible attributes to show, see the feature layer pages:
-
Create a
Popup
and save it to apopup
variable. Add it to the map withmap.add
.Overlay Use dark colors for code blocks const polygonLayer = new ol.layer.Vector({ source: polygonSource }); map.getLayers().insertAt(1, polygonLayer); const popup = new Popup(); map.addOverlay(popup);
-
Add a
click
event handler. Inside, check if a feature from the trailheads layer was clicked. If so, move the pop-up, and show the trailhead name and park name (TRL
and_NAME PARK
). Otherwise, check the trails layer; show the trail name, length and elevation gain (_NAME TRL
,_NAME LENGTH
,_MI ELEV
) if it was clicked. Otherwise, check the parks layer; show the park name and managing agency (_GAIN PARK
and_NAME MNG
) if it was clicked. Otherwise, hide the pop-up with_AGENCY popup.hide
;Use dark colors for code blocks const popup = new Popup(); map.addOverlay(popup); map.on("click", (event) => { let feature = map.getFeaturesAtPixel(event.pixel, { layerFilter: (l) => l === pointLayer })[0]; if (feature) { popup.show(event.coordinate, `<h4>${feature.get("TRL_NAME")}</h4>${feature.get("PARK_NAME")}`); return; } feature = map.getFeaturesAtPixel(event.pixel, { layerFilter: (l) => l === lineLayer })[0]; if (feature) { popup.show( event.coordinate, `<h4>${feature.get("TRL_NAME")}</h4>${feature.get("LENGTH_MI")} miles, ${feature.get("ELEV_GAIN")} feet elevation gain.` ); return; } feature = map.getFeaturesAtPixel(event.pixel, { layerFilter: (l) => l === polygonLayer })[0]; if (feature) { popup.show(event.coordinate, `<h4>${feature.get("PARK_NAME")}</h4>${feature.get("MNG_AGENCY")}`); return; } popup.hide(); });
Run the app
In CodePen, run your code to display the map.
You should now be able to click on features to see information in a pop-up.
What's next?
Learn how to use additional ArcGIS location services in these tutorials: