Learn how to add features
A feature layer
In this tutorial, you access and display three different hosted feature layers
The feature layers are:
Prerequisites
You need an ArcGIS Location Platform or ArcGIS Online account.
Steps
Get the starter app
Select a type of authentication and follow the steps to create a new app.
Choose API key authentication if you:
- Want the easiest way to get started.
- Want to build public applications
A public application is an application that allows anonymous access without requiring users to sign in with an ArcGIS account. It supports API key or app authentication. that access ArcGIS Location ServicesArcGIS Location Services, also referred to as Location Services, are services hosted by Esri that provide geospatial functionality for developing mapping applications. They include the ArcGIS Basemap Styles service, ArcGIS Static Basemap Tiles service, ArcGIS Places service, ArcGIS Geocoding service, ArcGIS Routing service, ArcGIS GeoEnrichment service, and ArcGIS Elevation service. An ArcGIS Location Platform or ArcGIS Online account is required to use the services. and secure itemsAn item, also known as a content item, is a resource stored in a portal such as a web map, hosted layer, style, script tool, file, or notebook. . - Have an ArcGIS Location Platform or ArcGIS Online account.
Choose user authentication if you:
- Want to build private applications.
- Require application users to sign in with their own ArcGIS account
An ArcGIS account is an identity with a user type and set of privileges that can access specific ArcGIS products, tools, APIs, services, and resources. The main account types that can be used for development are an ArcGIS Location Platform account, ArcGIS Online account, and ArcGIS Enterprise account. ArcGIS Location Platform and ArcGIS Online accounts are also associated with a subscription. and access resources their behalf. - Have an ArcGIS Online account.
To learn more about both types of authentication, go to Authentication.
Set up authentication
Set developer credentials
Use the API key or OAuth developer credentials
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
<headelement, add references to the ArcGIS REST JS and ol-popup libraries.> Use dark colors for code blocks <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/ol@v10.9.0/ol.css"> <script src="https://cdn.jsdelivr.net/npm/ol@v10.9.0/dist/ol.js"></script> <script src="https://cdn.jsdelivr.net/npm/ol-mapbox-style@13.4.1/dist/olms.js"></script> <script src="https://unpkg.com/@esri/arcgis-rest-request@4/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 layerx,y coordinates and a spatial reference.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 Geoas theJSON formatparameter. For theurlparameter, pass a direct link to a FeatureServer endpoint.The URL contains an
f=geojsonparameter to request data in GeoJSON format. For more information about querying a Feature ServiceA feature service is a data service that provides access to spatial and non-spatial data in feature layers, feature layer views, and tables. , see the Query a feature layer (SQL) and Query a feature layer (spatial) tutorials.Use dark colors for code blocks const basemapId = "arcgis/streets"; 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
Data attribution is the requirement to display the names of data source providers in all applications when accessing ArcGIS content, layers, or services. for the feature layer source.- Go to the Trailheads (Santa Monica Mountains) item
An item, also known as a content item, is a resource stored in a portal such as a web map, hosted layer, style, script tool, file, or notebook. . - Scroll down to the Acknowledgments section and copy its value.
- Paste the copied value to the
attributionsproperty.Use dark colors for code blocks const basemapId = "arcgis/streets"; 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 |"] });
- Go to the Trailheads (Santa Monica Mountains) item
-
Create a
Vectorlayer using theVectorsource you defined.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 });
Add a line feature layer
The Trails feature layerVector layer to access the feature layer by URL and display the lines. By default, the features are displayed as blue lines.
-
Create a
VectorwithSource formatandurlparameters.Use dark colors for code blocks const pointLayer = new ol.layer.Vector({ source: pointSource }); 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
Data attribution is the requirement to display the names of data source providers in all applications when accessing ArcGIS content, layers, or services. for the feature layer source.- Go to the Trails item
An item, also known as a content item, is a resource stored in a portal such as a web map, hosted layer, style, script tool, file, or notebook. . - Scroll down to the Acknowledgments section and copy its value.
- Paste the copied value to the
attributionsproperty.Use dark colors for code blocks const pointLayer = new ol.layer.Vector({ source: pointSource }); 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 |"] });
- Go to the Trails item
-
Create a
Vectorlayer using theVectorsource you defined.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 });
Add a polygon feature layer
The Parks and Open Space feature layerVector 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.
-
Create a
VectorwithSource formatandurlparameters.Use dark colors for code blocks const lineLayer = new ol.layer.Vector({ source: lineSource }); 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
Data attribution is the requirement to display the names of data source providers in all applications when accessing ArcGIS content, layers, or services. for the feature layer source.- Go to the Parks and Open Space item
An item, also known as a content item, is a resource stored in a portal such as a web map, hosted layer, style, script tool, file, or notebook. . - Scroll down to the Acknowledgments section and copy its value.
- Paste the copied value to the
attributionsproperty.Use dark colors for code blocks const lineLayer = new ol.layer.Vector({ source: lineSource }); 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 |"] });
- Go to the Parks and Open Space item
-
Create a
Vectorlayer using theVectorsource you defined.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 }); -
To display multiple layers in a specific order, you group them using a LayerGroup. Add all of the layers you created to the group, then display it on the map using
map.add.Layer() Use dark colors for code blocks const layerGroup = new ol.layer.Group({ layers: [polygonLayer, lineLayer, pointLayer] }); map.addLayer(layerGroup);
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
-
Create a
Popupand save it to apopupvariable. Add it to the map withmap.add.Overlay Use dark colors for code blocks const polygonLayer = new ol.layer.Vector({ source: polygonSource }); const layerGroup = new ol.layer.Group({ layers: [polygonLayer, lineLayer, pointLayer] }); map.addLayer(layerGroup); const popup = new Popup(); map.addOverlay(popup); -
Add a
clickevent 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 (TRLand_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 PARKand_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, `<strong>${feature.get("TRL_NAME")}</strong><br/>${feature.get("PARK_NAME")}`); return; } feature = map.getFeaturesAtPixel(event.pixel, { layerFilter: (l) => l === lineLayer })[0]; if (feature) { popup.show( event.coordinate, `<strong>${feature.get("TRL_NAME")}</strong><br/>${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, `<strong>${feature.get("PARK_NAME")}</strong><br/>${feature.get("MNG_AGENCY")}`); return; } popup.hide(); });
Run the app
Run the app.
You should now be able to click on features to see information in a pop-up.What's next?
Learn how to use additional location services in these tutorials: