Intro to layers

Note: Support for 3D on mobile devices may vary, view the system requirements for more information.

The layer is the most fundamental component of a Map. It is a collection of spatial data in the form of graphics or images that represent real-world phenomena. Layers may contain discrete features that store vector data or continuous cells/pixels that store raster data.

Maps may contain different types of layers. For a broad overview of each layer type available in the API, see this table in the class description for Layer.

All layers inherit properties, methods and events from Layer. Some of these common properties are discussed in this tutorial. To learn about properties specific to different layer types, search the samples for that layer type (e.g. SceneLayer).

Prior to completing the following steps, you should be familiar with views and Map. If necessary, complete the following tutorials first:

1. Create a Map, a SceneView and a checkbox input HTML element.

Create a basic Map and add it to a SceneView instance. Your JavaScript may look something like the code below:

require([
  "esri/Map",
  "esri/views/SceneView",
  "dojo/domReady!"
], function(
  Map,
  SceneView
) {

  // Create the Map
  var map = new Map({
    basemap: "oceans"
  });

  // Create the SceneView
  var view = new SceneView({
      container: "viewDiv",
      map: map
  });
});

Add a checkbox element to the body of your HTML. The purpose of this element will be discussed later in the tutorial.

<body>
  <div id="viewDiv"></div>
  <span id="layerToggle">
    <input type="checkbox" id="streetsLyr" checked> Transportation
  </span>
</body>

2. Create two layers using TileLayer

Create two instances of TileLayer prior to the code you wrote for creating a map and view. To do this, you must require the esri/layers/TileLayer module and specify the url property on the layer. The url property must point to a cached map service either hosted on ArcGIS Server or Portal for ArcGIS.

All layers used for accessing ArcGIS services have the url property, which must be set to render features in the view. In this sample we'll create layers for streets and highways using the Esri World Transportation service and a service containing data about housing density in New York City.

require([
    "esri/Map",
    "esri/views/SceneView",
    "esri/layers/TileLayer",  // Require the TileLayer module
    "dojo/domReady!"
  ],
  function(
    Map, SceneView, TileLayer
  ) {

    var transportationLyr = new TileLayer({
      url: "https://server.arcgisonline.com/ArcGIS/rest/services/Reference/World_Transportation/MapServer"
    });

    var housingLyr = new TileLayer({
      url: "https://tiles.arcgis.com/tiles/nGt4QxSblgDfeJn9/arcgis/rest/services/New_York_Housing_Density/MapServer"
    });

    /*****************************************************************
    * The code to create a map and view instance in the previous step
    * should be placed here.
    *****************************************************************/
});

3. Set additional properties on the layers

You may set additional properties on the layers including an id, minScale, maxScale, opacity, and visible. These may either be set in the constructor or directly on the instance at another point in your application.

We'll add an id to each layer and set the opacity on the transportation layer.

var transportationLyr = new TileLayer({
  url: "https://server.arcgisonline.com/ArcGIS/rest/services/Reference/World_Transportation/MapServer",
  id: "streets",
  opacity: 0.7
});

var housingLyr = new TileLayer({
  url: "https://tiles.arcgis.com/tiles/nGt4QxSblgDfeJn9/arcgis/rest/services/New_York_Housing_Density/MapServer",
  id: "ny-housing"
});

The id uniquely identifies the layer, making it easy to reference in other parts of the application. If this isn't set directly by the developer, then it is automatically generated when the layer is created.

The minScale and maxScale properties control the visibility of the layer at various scales. Using these properties can improve app performance at certain scales and enhance the cartography of the map. The visible property is true by default.

4. Add the layers to the map

Layers may be added to the map in several ways. These are all discussed in the documentation for Map.layers. In this sample we'll add each layer to the map in a different way.

Add the housing layer to the map's constructor.

// Both layers were created prior to this code snippet
var map = new Map({
  basemap: "oceans",
  layers: [housingLyr]  // layers can be added as an array to the map's constructor
});

Add the transportation layer to the map using map.layers.add().

map.layers.add(transportationLyr);

Now both layers should be visible in the view.

5. Work with a layer's visibility

Require the dojo/on and dojo/dom modules to listen to a change event on the checkbox element created in the first step. When the box is checked on and off it will toggle the visibility of the transportation layer on and off. Like visible, any property of any layer may be set directly on the layer instance. This is accomplished in the snippet below.

require([
    "esri/Map",
    "esri/views/SceneView",
    "esri/layers/TileLayer",
    "dojo/dom",  // require dojo/dom for getting the DOM element
    "dojo/on",   // require dojo/on for listening to events on the DOM
    "dojo/domReady!"
  ],
  function(
    Map, SceneView, TileLayer, dom, on
  ) {

    /*****************************************************************
     * All code previously written in the steps above should be placed
     * before the following code
     *******************************************************************/

    // Create a variable referencing the checkbox node
    var streetsLyrToggle = dom.byId("streetsLyr");

    // Listen to the onchange event for the checkbox
    on(streetsLyrToggle, "change", function(){
      // When the checkbox is checked (true), set the layer's visibility to true
      transportationLyr.visible = streetsLyrToggle.checked;
    });
  });

Even though the layer is not visible to the view in this sample, it still exists as part of the map. Therefore, you can still access all of the properties of the layer and use them for analysis even though the user may not see the layer rendered in the view.

6. Understanding LayerViews

The Layer object manages geographic and tabular data published as a service. It does not handle rendering the layer in the View. That is the job of the LayerView. A Layer's LayerView is created just before it renders in the view. When working with FeatureLayers, the corresponding FeatureLayerView can provide the developer with access to the graphics rendered in the view related to that layer's features.

In this step, we'll listen for the view's layerview-create event and print the LayerViews of the housing and the transportation layers so you can explore their properties in the console. Notice that we'll use the id we created for each layer in step 3 to get the desired layers. In addition to a map's operational layers, this event fires for basemap layers and elevation layers.

require([
    "esri/Map",
    "esri/views/SceneView",
    "esri/layers/TileLayer",
    "dojo/dom",
    "dojo/on",
    "dojo/domReady!"
  ],
  function(
    Map, SceneView, TileLayer, dom, on
  ) {

    /*****************************************************************
     * All code previously written in the steps above should be placed
     * before the following code
     *******************************************************************/

    // This event fires each time a layer's LayerView is created for the
    // specified view instance
    view.on("layerview-create", function(event) {
      if (event.layer.id === "ny-housing") {
        // Explore the properties of the housing layer's layer view here
        console.log("LayerView for New York housing density created!", event.layerView);
      }
      if (event.layer.id === "streets") {
        // Explore the properties of the transportation layer's layer view here
        console.log("LayerView for streets created!", event.layerView);
      }
    });

  });

7. Using Layer.then()

A layer is a promise that resolves when loaded, or when all its properties are available to the developer. In this sample, we want to animate the view to the fullExtent of the housing layer since we may not know a good extent (or center and zoom) to use for initializing the view.

We cannot get the layer's fullExtent until it has loaded. So we must handle the animation once the promise resolves. This is handled using then().

// When the layer's promise resolves, animate the view to the layer's fullExtent
housingLyr.then(function() {
  view.goTo(housingLyr.fullExtent);
});

8. Summary

There are many more properties of Layer that have not been discussed here. To learn more about additional properties related to specific layers, see the API documentation and samples listed in the table of contents on this page. The final code for this tutorial can be reviewed in the sandbox.

Sample search results

TitleSample

There were no match results from your search criteria.