Overview
You will learn: how to build an app that loads and displays a layer from ArcGIS Online.
Applications can access layer items that are hosted on
In this tutorial, you will add layers to the map that have been pre-configured and stored in ArcGIS Online.
Zoom in and click the map below to see the pre-configured styles and pop-ups.
Steps
Create a starter app
Open the JavaScript Starter App on CodePen.
In CodePen, click Fork and save the pen as
ArcGIS JavaScript Tutorials: Add a layer from an item
.
Add the Trailheads layer by id
In your browser, go to the Trailheads layer on
ArcGIS Online and find the item ID at the end of the URL. It should be33fc2fa407ab40f9add12fe38d5801f5
.In the
require
statement, add a reference to theLayer
module.require([ "esri/Map", "esri/views/MapView", "esri/layers/Layer" ], function(Map, MapView, Layer) {
At the end of the code in the main
function
, define a function that will be used to add layers to the map. The function will wait for theportalItem
to be loaded and then will add the layer to the map in the specifiedindex
position.function addLayer(layerItemPromise, index) { return layerItemPromise.then(function(layer){ map.add(layer, index); }); }
Use the static member
Layer
and the fromPortalItem method to access the Trailheads (points) layer. Set theid
property of theportalItem
to33fc2fa407ab40f9add12fe38d5801f5
. Use theaddLayer
function to add the layer to the map.var trailheadsPortalItem = Layer.fromPortalItem({ portalItem: { id: "33fc2fa407ab40f9add12fe38d5801f5" } }); addLayer(trailheadsPortalItem, 0);
Run your code to view the layer.
Add the Trails and Parks and Open Space layers by id
Use the fromPortalItem method again to load the Trails (lines) and Parks and Open Spaces (polygons) layers by id. Use the
addLayer
function to load the layers in the correct drawing order.var trailheadsPortalItem = Layer.fromPortalItem({ portalItem: { id: "33fc2fa407ab40f9add12fe38d5801f5" } }); //*** ADD ***// // Trails var trailsPortalItem = Layer.fromPortalItem({ portalItem: { id: "52a162056a2d48409fc3b3cbb672e7da" } }); // Parks var parksPortalItem = Layer.fromPortalItem({ portalItem: { id: "83cf97eea04e4a699689c250dd07b975" } }); //*** ADD ***// addLayer(trailheadsPortalItem, 2) .then(addLayer(trailsPortalItem, 1)) .then(addLayer(parksPortalItem, 0));
Run your code to view the map. You should see three layers with pre-configured styles. Click on the features to view the pre-configured pop-ups.
Learn how to pre-configuring layers in the Configured layers tutorial and learn how to style feature layers programmatically in the Style feature layers tutorial.
Congratulations, you're done!
Your app should look something like this.
Challenge
Limit layer features with a SQL definition expression
You can update layer properties by setting the values before it is displayed. For example, you can update the popup
, renderer
, or definitionExpression
. Update the layer to apply a SQL definition expression to limit the data returned from a layer (after it is loaded). Update the addLayer
function to define an expression that limits the Trails to only those with an elevation gain less than 250 ft. Explore the data and try defining your own expressions.
function addLayer(layerItemPromise, index) {
return layerItemPromise.then(function(layer){
layer.when(function(){
if (layer.title === "Trails_Styled_Popups") {
layer.definitionExpression = "ELEV_GAIN < 250";
}
});
map.add(layer, index);
});
}
Learn more about definition expressions and supported SQL queries in the documentation.