Learn how to access and display point, line, and polygon features in feature layers.
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 use feature layers to store, access, and manage large amounts of geographic data for your applications. You get features from a feature layer by accessing its URL.
In this tutorial, you will use URLs to access and display three different hosted feature layers:
Prerequisites
Steps
Create a new pen
- To get started, either complete the Display a map tutorial or .
Get an access token
You need an access token with the correct privileges to access the location services 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
- In CodePen, set
esri
to your access token.Config.api Key Use dark colors for code blocks var esriConfig = { apiKey: "YOUR_ACCESS_TOKEN" };
To learn about other ways to get an access token, go to Types of authentication.
Add modules
-
In a new
<script
at the bottom of the> <body
, use a> require
statement to add theFeature
module.Layer The ArcGIS Maps SDK for JavaScript is available as AMD modules and ES modules, but this tutorial is based on AMD. The AMD
require
function uses references to determine which modules will be loaded – for example, you can specify"esri/layers/
for loading the FeatureLayer module. After the modules are loaded, they are passed as parameters (e.g.Feature Layer" Feature
) to the callback function where they can be used in your application. It is important to keep the module references and callback parameters in the same order. To learn more about the API's different modules visit the Overview Guide page.Layer Use dark colors for code blocks <script> require(["esri/layers/FeatureLayer"], (FeatureLayer) => { }); </script>
Add a point feature layer
Point features are typically displayed in a feature layer on top of all other layers. Use the Feature
class to reference the Trailheads URL and add features to the map.
-
Go to the Trailheads URL and browse the properties of the layer. Make note of the Name, Type, Drawing Info, and Fields properties.
-
In CodePen, create a
Feature
and set theLayer url
property. When using the arcgis-map component, you need to add an event listener forarcgis
to wait until the component is ready before adding the layer.View Ready Change Use dark colors for code blocks <script> require(["esri/layers/FeatureLayer"], (FeatureLayer) => { const arcgisMap = document.querySelector("arcgis-map"); arcgisMap.addEventListener("arcgisViewReadyChange", () => { // Trailheads feature layer (points) const trailheadsLayer = new FeatureLayer({ url: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Trailheads/FeatureServer/0" }); }); }); </script>
-
Add
trailheads
to the map.Layer Use dark colors for code blocks // Trailheads feature layer (points) const trailheadsLayer = new FeatureLayer({ url: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Trailheads/FeatureServer/0" }); arcgisMap.addLayer(trailheadsLayer);
-
Run the app to view the Trailheads layer in the map.
Add a line feature layer
Line features are typically displayed in a feature layer before points. Use the Feature
class to reference the Trails URL and add features to the map.
-
Go to the Trails URL and browse the properties of the layer. Make note of the Name, Type, Drawing Info, and Fields properties.
-
In CodePen, create a
Feature
and set theLayer url
property.Use dark colors for code blocks arcgisMap.addLayer(trailheadsLayer); // Trails feature layer (lines) const trailsLayer = new FeatureLayer({ url: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Trails/FeatureServer/0" });
-
Add
trails
to the map with an index ofLayer 0
. This ensures that the layer is added to the top of the array and is drawn beforetrailheads
.Layer Use dark colors for code blocks // Trails feature layer (lines) const trailsLayer = new FeatureLayer({ url: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Trails/FeatureServer/0" }); arcgisMap.addLayer(trailsLayer, 0);
-
Run the app to view the Trails layer in the map.
Add a polygon feature layer
Polygon features are typically displayed in a feature layer before lines. Use the Feature
class to reference the Parks and Open Spaces URL and add features to the map.
-
Go to the Parks and Open Spaces URL and browse the properties of the layer. Make note of the Name, Type, Drawing Info, and Fields properties.
-
In CodePen, create a
Feature
and set theLayer url
property.Use dark colors for code blocks arcgisMap.addLayer(trailsLayer, 0); // Parks and open spaces (polygons) const parksLayer = new FeatureLayer({ url: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Parks_and_Open_Space/FeatureServer/0" });
-
Add
parks
to the map with an index ofLayer 0
. This ensures that the layer is added to the top of the array and is drawn beforetrails
.Layer Use dark colors for code blocks // Parks and open spaces (polygons) const parksLayer = new FeatureLayer({ url: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Parks_and_Open_Space/FeatureServer/0" }); arcgisMap.addLayer(parksLayer, 0);
Run the app
In CodePen, run your code to display the map.
The map should display all three feature layers in the map in following order:
- Topographic basemap layer
- Parks and Open Spaces (polygons)
- Trails (lines)
- Trailheads (points)
It is important to add feature layers in the correct order so that features are displayed correctly (not overlapping) and so you can interact with the features.
What's next?
Learn how to use additional API features and ArcGIS services in these tutorials: