###Add a feature layer to a map
In this lab you will add a feature layer to an ArcGIS API for JavaScript application.
- Explore the FeatureLayer Class and its corresponding properties
- Explore the SceneView Class and its corresponding properties
- Explore the Map Class and its corresponding properties
-
Click create_starter_map/index.html and copy the contents to a new jsbin.com.
-
In
JSBin
>HTML
, update therequire
statement andfunction
definition:
require([
"esri/Map",
"esri/views/MapView",
/*** ADD ***/
"esri/layers/FeatureLayer",
"dojo/domReady!"
], function(Map, MapView, FeatureLayer) {
- Now add the NYC Metro Lines to the map by assigning the REST endpoint url of the layer.
...
/*** ADD ***/
var metrolines = new FeatureLayer({
url: "https://services.arcgis.com/bGgB6gXiQ835YdNp/arcgis/rest/services/NYC_SubwayLines/FeatureServer/1"
});
map.add(metrolines);
- Confirm that the JSBin
Output
panel shows a map with rail lines.
Your app should look something like this:
###Bonus
- Add a Metro Stations feature layer to the map, and then add a BlockGroups feature layer.
- Ensure the layers are ordered with polygons on the bottom, lines and then points on top.
- If you added additional layers using the
add()
method, try theaddMany()
method instead code for addMany. Here is example code of adding layers in the constructor for the map. Read up on thelayers
collection and see how the API gives you a few ways to add layers to a map. *'map.addMany([nyc_blocks, metrolines, stations]);' - Watch for a property change on a feature layer loading. Check the API for examples of how to watch for property changes. In the jsbin.com you will want to open the console so you can see the output. Here is the example code.
metroStationsFL.watch("loadStatus", function(status) {
// status types not-loaded, loading, loaded, failed
console.log("'" + metroStationsFL.title + "'" + " " + status);
if (status === "failed") {
console.log(poi.loadError);
}
- The 4.x JS API works closely with ArcGIS Portals. Instead of the Feature Service URL, use the Portal Item ID to add layers. See the URLs of the Portal Items for Stations, Lines and BlockGroups to get their IDs. Here is the code.. What functionality do you gain in the app automatically when using the Portal Id's vs adding the feature layer using the url?