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 use URLs to access and display three different hosted feature layers:
You need a free ArcGIS developer account to access your dashboard and API keys. The API key must be scoped to access the services used in this tutorial.
In the require statement, add the FeatureLayer module.
The ArcGIS API for JavaScript uses AMD modules. The require function is used to load modules so they can be used in the main function. It's important to keep the module references and function parameters in the same order.
Add line.Change line
<html><head><metacharset="utf-8"><metaname="viewport"content="initial-scale=1, maximum-scale=1, user-scalable=no"><title>ArcGIS API for JavaScript Tutorials: Add a feature layer</title><style>html, body, #viewDiv {
padding: 0;
margin: 0;
height: 100%;
width: 100%;
}
</style><linkrel="stylesheet"href="https://js.arcgis.com/4.18/esri/themes/light/main.css"><scriptsrc="https://js.arcgis.com/4.18/"></script><script>require([
"esri/config",
"esri/Map",
"esri/views/MapView",
"esri/layers/FeatureLayer" ], function(esriConfig,Map, MapView, FeatureLayer) {
esriConfig.apiKey = "YOUR-API-KEY";
const map = newMap({
basemap: "arcgis-topographic" });
const view = new MapView({
container: "viewDiv",
map: map,
center: [-118.80543,34.02700],
zoom: 13 });
//Trailheads feature layer (points)const trailheadsLayer = new FeatureLayer({
url: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Trailheads_Styled/FeatureServer/0" });
map.add(trailheadsLayer);
//Trails feature layer (lines)const trailsLayer = new FeatureLayer({
url: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Trails_Styled/FeatureServer/0" });
map.add(trailsLayer, 0);
// Parks and open spaces (polygons)const parksLayer = new FeatureLayer({
url: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Parks_and_Open_Space_Styled/FeatureServer/0" });
map.add(parksLayer, 0);
});
</script></head><body><divid="viewDiv"></div></body></html>
Add a point feature layer
Point features are typically displayed in a feature layer on top of all other layers. Use the FeatureLayer 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.
<html><head><metacharset="utf-8"><metaname="viewport"content="initial-scale=1, maximum-scale=1, user-scalable=no"><title>ArcGIS API for JavaScript Tutorials: Add a feature layer</title><style>html, body, #viewDiv {
padding: 0;
margin: 0;
height: 100%;
width: 100%;
}
</style><linkrel="stylesheet"href="https://js.arcgis.com/4.18/esri/themes/light/main.css"><scriptsrc="https://js.arcgis.com/4.18/"></script><script>require([
"esri/config",
"esri/Map",
"esri/views/MapView",
"esri/layers/FeatureLayer" ], function(esriConfig,Map, MapView, FeatureLayer) {
esriConfig.apiKey = "YOUR-API-KEY";
const map = newMap({
basemap: "arcgis-topographic" });
const view = new MapView({
container: "viewDiv",
map: map,
center: [-118.80543,34.02700],
zoom: 13 });
//Trailheads feature layer (points)const trailheadsLayer = new FeatureLayer({
url: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Trailheads_Styled/FeatureServer/0" });
map.add(trailheadsLayer);
//Trails feature layer (lines)const trailsLayer = new FeatureLayer({
url: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Trails_Styled/FeatureServer/0" });
map.add(trailsLayer, 0);
// Parks and open spaces (polygons)const parksLayer = new FeatureLayer({
url: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Parks_and_Open_Space_Styled/FeatureServer/0" });
map.add(parksLayer, 0);
});
</script></head><body><divid="viewDiv"></div></body></html>
Add trailheadsLayer to the map.
Add line.
<html><head><metacharset="utf-8"><metaname="viewport"content="initial-scale=1, maximum-scale=1, user-scalable=no"><title>ArcGIS API for JavaScript Tutorials: Add a feature layer</title><style>html, body, #viewDiv {
padding: 0;
margin: 0;
height: 100%;
width: 100%;
}
</style><linkrel="stylesheet"href="https://js.arcgis.com/4.18/esri/themes/light/main.css"><scriptsrc="https://js.arcgis.com/4.18/"></script><script>require([
"esri/config",
"esri/Map",
"esri/views/MapView",
"esri/layers/FeatureLayer" ], function(esriConfig,Map, MapView, FeatureLayer) {
esriConfig.apiKey = "YOUR-API-KEY";
const map = newMap({
basemap: "arcgis-topographic" });
const view = new MapView({
container: "viewDiv",
map: map,
center: [-118.80543,34.02700],
zoom: 13 });
//Trailheads feature layer (points)const trailheadsLayer = new FeatureLayer({
url: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Trailheads_Styled/FeatureServer/0" });
map.add(trailheadsLayer);
//Trails feature layer (lines)const trailsLayer = new FeatureLayer({
url: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Trails_Styled/FeatureServer/0" });
map.add(trailsLayer, 0);
// Parks and open spaces (polygons)const parksLayer = new FeatureLayer({
url: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Parks_and_Open_Space_Styled/FeatureServer/0" });
map.add(parksLayer, 0);
});
</script></head><body><divid="viewDiv"></div></body></html>
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 FeatureLayer 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.
<html><head><metacharset="utf-8"><metaname="viewport"content="initial-scale=1, maximum-scale=1, user-scalable=no"><title>ArcGIS API for JavaScript Tutorials: Add a feature layer</title><style>html, body, #viewDiv {
padding: 0;
margin: 0;
height: 100%;
width: 100%;
}
</style><linkrel="stylesheet"href="https://js.arcgis.com/4.18/esri/themes/light/main.css"><scriptsrc="https://js.arcgis.com/4.18/"></script><script>require([
"esri/config",
"esri/Map",
"esri/views/MapView",
"esri/layers/FeatureLayer" ], function(esriConfig,Map, MapView, FeatureLayer) {
esriConfig.apiKey = "YOUR-API-KEY";
const map = newMap({
basemap: "arcgis-topographic" });
const view = new MapView({
container: "viewDiv",
map: map,
center: [-118.80543,34.02700],
zoom: 13 });
//Trailheads feature layer (points)const trailheadsLayer = new FeatureLayer({
url: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Trailheads_Styled/FeatureServer/0" });
map.add(trailheadsLayer);
//Trails feature layer (lines)const trailsLayer = new FeatureLayer({
url: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Trails_Styled/FeatureServer/0" });
map.add(trailsLayer, 0);
// Parks and open spaces (polygons)const parksLayer = new FeatureLayer({
url: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Parks_and_Open_Space_Styled/FeatureServer/0" });
map.add(parksLayer, 0);
});
</script></head><body><divid="viewDiv"></div></body></html>
Add trailsLayer to the map with an index of 0. This ensures that the layer is added to the top of the array and is drawn before trailheadsLayer.
Add line.
<html><head><metacharset="utf-8"><metaname="viewport"content="initial-scale=1, maximum-scale=1, user-scalable=no"><title>ArcGIS API for JavaScript Tutorials: Add a feature layer</title><style>html, body, #viewDiv {
padding: 0;
margin: 0;
height: 100%;
width: 100%;
}
</style><linkrel="stylesheet"href="https://js.arcgis.com/4.18/esri/themes/light/main.css"><scriptsrc="https://js.arcgis.com/4.18/"></script><script>require([
"esri/config",
"esri/Map",
"esri/views/MapView",
"esri/layers/FeatureLayer" ], function(esriConfig,Map, MapView, FeatureLayer) {
esriConfig.apiKey = "YOUR-API-KEY";
const map = newMap({
basemap: "arcgis-topographic" });
const view = new MapView({
container: "viewDiv",
map: map,
center: [-118.80543,34.02700],
zoom: 13 });
//Trailheads feature layer (points)const trailheadsLayer = new FeatureLayer({
url: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Trailheads_Styled/FeatureServer/0" });
map.add(trailheadsLayer);
//Trails feature layer (lines)const trailsLayer = new FeatureLayer({
url: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Trails_Styled/FeatureServer/0" });
map.add(trailsLayer, 0);
// Parks and open spaces (polygons)const parksLayer = new FeatureLayer({
url: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Parks_and_Open_Space_Styled/FeatureServer/0" });
map.add(parksLayer, 0);
});
</script></head><body><divid="viewDiv"></div></body></html>
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 FeatureLayer 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.
<html><head><metacharset="utf-8"><metaname="viewport"content="initial-scale=1, maximum-scale=1, user-scalable=no"><title>ArcGIS API for JavaScript Tutorials: Add a feature layer</title><style>html, body, #viewDiv {
padding: 0;
margin: 0;
height: 100%;
width: 100%;
}
</style><linkrel="stylesheet"href="https://js.arcgis.com/4.18/esri/themes/light/main.css"><scriptsrc="https://js.arcgis.com/4.18/"></script><script>require([
"esri/config",
"esri/Map",
"esri/views/MapView",
"esri/layers/FeatureLayer" ], function(esriConfig,Map, MapView, FeatureLayer) {
esriConfig.apiKey = "YOUR-API-KEY";
const map = newMap({
basemap: "arcgis-topographic" });
const view = new MapView({
container: "viewDiv",
map: map,
center: [-118.80543,34.02700],
zoom: 13 });
//Trailheads feature layer (points)const trailheadsLayer = new FeatureLayer({
url: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Trailheads_Styled/FeatureServer/0" });
map.add(trailheadsLayer);
//Trails feature layer (lines)const trailsLayer = new FeatureLayer({
url: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Trails_Styled/FeatureServer/0" });
map.add(trailsLayer, 0);
// Parks and open spaces (polygons)const parksLayer = new FeatureLayer({
url: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Parks_and_Open_Space_Styled/FeatureServer/0" });
map.add(parksLayer, 0);
});
</script></head><body><divid="viewDiv"></div></body></html>
Add parksLayer to the map with an index of 0. This ensures that the layer is added to the top of the array and is drawn before trailsLayer.
Add line.
<html><head><metacharset="utf-8"><metaname="viewport"content="initial-scale=1, maximum-scale=1, user-scalable=no"><title>ArcGIS API for JavaScript Tutorials: Add a feature layer</title><style>html, body, #viewDiv {
padding: 0;
margin: 0;
height: 100%;
width: 100%;
}
</style><linkrel="stylesheet"href="https://js.arcgis.com/4.18/esri/themes/light/main.css"><scriptsrc="https://js.arcgis.com/4.18/"></script><script>require([
"esri/config",
"esri/Map",
"esri/views/MapView",
"esri/layers/FeatureLayer" ], function(esriConfig,Map, MapView, FeatureLayer) {
esriConfig.apiKey = "YOUR-API-KEY";
const map = newMap({
basemap: "arcgis-topographic" });
const view = new MapView({
container: "viewDiv",
map: map,
center: [-118.80543,34.02700],
zoom: 13 });
//Trailheads feature layer (points)const trailheadsLayer = new FeatureLayer({
url: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Trailheads_Styled/FeatureServer/0" });
map.add(trailheadsLayer);
//Trails feature layer (lines)const trailsLayer = new FeatureLayer({
url: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Trails_Styled/FeatureServer/0" });
map.add(trailsLayer, 0);
// Parks and open spaces (polygons)const parksLayer = new FeatureLayer({
url: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Parks_and_Open_Space_Styled/FeatureServer/0" });
map.add(parksLayer, 0);
});
</script></head><body><divid="viewDiv"></div></body></html>
Run the app
In CodePen, run your code to display the map.
The map view should display all three feature layers in the map. The map view draws 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.