Learn how to access and display
Zoom and pan the map to explore the trailheads, trails, and open spaces feature layers.
A
In this tutorial, you will use URLs to access and display three different hosted
Prerequisites
Steps
Create a new pen
- To get started, either complete the Display a map tutorial or
.
Get an access token
You need an
- Go to the Create an API key tutorial and create an
API key with the followingprivilege(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.
- Note: If you are using your own custom data layer for this tutorial, you need to grant the
- Privileges
- In CodePen, set the
apiKeyproperty on the globalesriConfigvariable to your access token.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$arcgis.importto add theFeatureLayermodule.The
ArcGIS Maps SDK for JavaScript is available via CDN and npm, but this tutorial is based on CDN. The$arcgis.importglobal function accepts a module path or array of module paths, and returns a promise that resolves with the requested modules. This function can only be used when working with the CDN; otherwise, use the standard import syntax. To learn more about the SDK’s different modules, visit the References page.30 collapsed lines<html><head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /><title>ArcGIS Maps SDK for JavaScript Tutorials: Add a feature layer</title><style>html,body {height: 100%;margin: 0;}</style><script>var esriConfig = { apiKey: "YOUR_ACCESS_TOKEN" };</script><!-- Load the ArcGIS Maps SDK for JavaScript from CDN --><script type="module" src="https://js.arcgis.com/5.0/"></script></head><body><arcgis-map basemap="arcgis/topographic" center="-118.805, 34.020" zoom="13"><arcgis-zoom slot="top-left"></arcgis-zoom></arcgis-map><script type="module">const FeatureLayer = await $arcgis.import("@arcgis/core/layers/FeatureLayer.js");</script>4 collapsed lines</body></html>
Add a point feature layer
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.
-
In CodePen, use
document.querySelector()to get a reference to the Map component, then wait for it to be ready with the viewOnReady method. Next, create aFeatureLayerand set theurlproperty.30 collapsed lines<html><head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /><title>ArcGIS Maps SDK for JavaScript Tutorials: Add a feature layer</title><style>html,body {height: 100%;margin: 0;}</style><script>var esriConfig = { apiKey: "YOUR_ACCESS_TOKEN" };</script><!-- Load the ArcGIS Maps SDK for JavaScript from CDN --><script type="module" src="https://js.arcgis.com/5.0/"></script></head><body><arcgis-map basemap="arcgis/topographic" center="-118.805, 34.020" zoom="13"><arcgis-zoom slot="top-left"></arcgis-zoom></arcgis-map><script type="module">const FeatureLayer = await $arcgis.import("@arcgis/core/layers/FeatureLayer.js");const viewElement = document.querySelector("arcgis-map");await viewElement.viewOnReady();// Trailheads feature layer (points)const trailheadsLayer = new FeatureLayer({url: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Trailheads/FeatureServer/0",});</script>4 collapsed lines</body></html> -
Add
trailheadsLayerto the map.36 collapsed lines<html><head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /><title>ArcGIS Maps SDK for JavaScript Tutorials: Add a feature layer</title><style>html,body {height: 100%;margin: 0;}</style><script>var esriConfig = { apiKey: "YOUR_ACCESS_TOKEN" };</script><!-- Load the ArcGIS Maps SDK for JavaScript from CDN --><script type="module" src="https://js.arcgis.com/5.0/"></script></head><body><arcgis-map basemap="arcgis/topographic" center="-118.805, 34.020" zoom="13"><arcgis-zoom slot="top-left"></arcgis-zoom></arcgis-map><script type="module">const FeatureLayer = await $arcgis.import("@arcgis/core/layers/FeatureLayer.js");const viewElement = document.querySelector("arcgis-map");await viewElement.viewOnReady();// Trailheads feature layer (points)const trailheadsLayer = new FeatureLayer({url: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Trailheads/FeatureServer/0",});viewElement.map.add(trailheadsLayer);6 collapsed lines</script></body></html> -
Run the app to view the Trailheads
layer in the map.
Add a line feature layer
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. -
In CodePen, create a
FeatureLayerand set theurlproperty.41 collapsed lines<html><head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /><title>ArcGIS Maps SDK for JavaScript Tutorials: Add a feature layer</title><style>html,body {height: 100%;margin: 0;}</style><script>var esriConfig = { apiKey: "YOUR_ACCESS_TOKEN" };</script><!-- Load the ArcGIS Maps SDK for JavaScript from CDN --><script type="module" src="https://js.arcgis.com/5.0/"></script></head><body><arcgis-map basemap="arcgis/topographic" center="-118.805, 34.020" zoom="13"><arcgis-zoom slot="top-left"></arcgis-zoom></arcgis-map><script type="module">const FeatureLayer = await $arcgis.import("@arcgis/core/layers/FeatureLayer.js");const viewElement = document.querySelector("arcgis-map");await viewElement.viewOnReady();// Trailheads feature layer (points)const trailheadsLayer = new FeatureLayer({url: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Trailheads/FeatureServer/0",});viewElement.map.add(trailheadsLayer);// Trails feature layer (lines)const trailsLayer = new FeatureLayer({url: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Trails/FeatureServer/0",});6 collapsed lines</script></body></html> -
Add
trailsLayerto the map with an index of0. This ensures that the layer is added to the top of the array and is drawn beforetrailheadsLayer.43 collapsed lines<html><head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /><title>ArcGIS Maps SDK for JavaScript Tutorials: Add a feature layer</title><style>html,body {height: 100%;margin: 0;}</style><script>var esriConfig = { apiKey: "YOUR_ACCESS_TOKEN" };</script><!-- Load the ArcGIS Maps SDK for JavaScript from CDN --><script type="module" src="https://js.arcgis.com/5.0/"></script></head><body><arcgis-map basemap="arcgis/topographic" center="-118.805, 34.020" zoom="13"><arcgis-zoom slot="top-left"></arcgis-zoom></arcgis-map><script type="module">const FeatureLayer = await $arcgis.import("@arcgis/core/layers/FeatureLayer.js");const viewElement = document.querySelector("arcgis-map");await viewElement.viewOnReady();// Trailheads feature layer (points)const trailheadsLayer = new FeatureLayer({url: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Trailheads/FeatureServer/0",});viewElement.map.add(trailheadsLayer);// Trails feature layer (lines)const trailsLayer = new FeatureLayer({url: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Trails/FeatureServer/0",});viewElement.map.add(trailsLayer, 0);6 collapsed lines</script></body></html> -
Run the app to view the Trails
layer in the map.
Add a polygon feature layer
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.
-
In CodePen, create a
FeatureLayerand set theurlproperty.48 collapsed lines<html><head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /><title>ArcGIS Maps SDK for JavaScript Tutorials: Add a feature layer</title><style>html,body {height: 100%;margin: 0;}</style><script>var esriConfig = { apiKey: "YOUR_ACCESS_TOKEN" };</script><!-- Load the ArcGIS Maps SDK for JavaScript from CDN --><script type="module" src="https://js.arcgis.com/5.0/"></script></head><body><arcgis-map basemap="arcgis/topographic" center="-118.805, 34.020" zoom="13"><arcgis-zoom slot="top-left"></arcgis-zoom></arcgis-map><script type="module">const FeatureLayer = await $arcgis.import("@arcgis/core/layers/FeatureLayer.js");const viewElement = document.querySelector("arcgis-map");await viewElement.viewOnReady();// Trailheads feature layer (points)const trailheadsLayer = new FeatureLayer({url: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Trailheads/FeatureServer/0",});viewElement.map.add(trailheadsLayer);// Trails feature layer (lines)const trailsLayer = new FeatureLayer({url: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Trails/FeatureServer/0",});viewElement.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/FeatureServer/0",});6 collapsed lines</script></body></html> -
Add
parksLayerto the map with an index of0. This ensures that the layer is added to the top of the array and is drawn beforetrailsLayer.50 collapsed lines<html><head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /><title>ArcGIS Maps SDK for JavaScript Tutorials: Add a feature layer</title><style>html,body {height: 100%;margin: 0;}</style><script>var esriConfig = { apiKey: "YOUR_ACCESS_TOKEN" };</script><!-- Load the ArcGIS Maps SDK for JavaScript from CDN --><script type="module" src="https://js.arcgis.com/5.0/"></script></head><body><arcgis-map basemap="arcgis/topographic" center="-118.805, 34.020" zoom="13"><arcgis-zoom slot="top-left"></arcgis-zoom></arcgis-map><script type="module">const FeatureLayer = await $arcgis.import("@arcgis/core/layers/FeatureLayer.js");const viewElement = document.querySelector("arcgis-map");await viewElement.viewOnReady();// Trailheads feature layer (points)const trailheadsLayer = new FeatureLayer({url: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Trailheads/FeatureServer/0",});viewElement.map.add(trailheadsLayer);// Trails feature layer (lines)const trailsLayer = new FeatureLayer({url: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Trails/FeatureServer/0",});viewElement.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/FeatureServer/0",});viewElement.map.add(parksLayer, 0);6 collapsed lines</script></body></html>
Run the app
In CodePen, run your code to display the map.
The
- 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 SDK features and ArcGIS services in these tutorials: