Display a web scene

A web scene is a scene that is stored as an item in ArcGIS Online. A web scene item contains all of the configuration settings for the scene (in JSON format) such as extent, basemap, data layers, and styles. Applications can access and display the web scene using its item ID.

In this tutorial, you access and display a pre-configured web scene stored in ArcGIS Online.

Prerequisites

You need a free ArcGIS developer account to access your dashboard and API keys. The API key must be scoped to access the services used in this tutorial.

Steps

Create a new pen

  1. To get started, either complete the Display a map tutorial or .

Set the API key

To access ArcGIS services, you need an API key.

  1. Go to your dashboard to get an API key.

  2. In CodePen, set the apiKey to your key, so it can be used to access basemap layer and location services.

    Change line
        
    esriConfig.apiKey = "YOUR-API-KEY";
    const map = new Map({
      basemap: "arcgis-topographic" //Basemap layer service
    });
    

Add modules

  1. In the require statement, delete the Map and MapView modules. Add the WebScene, SceneView, and Legend modules.

    The ArcGIS API for JavaScript uses AMD modules. The require function is used to load modules so they can be used in the main function. It's important to keep the module references and function parameters in the same order.

    Remove lineRemove lineAdd line.Add line.Add line.Change line
                                                                      
    <html>
    <head>
      <meta charset="utf-8">
      <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no">
      <title>ArcGIS API for JavaScript Tutorials: Display a web scene</title>
      <style>
        html, body, #viewDiv {
          padding: 0;
          margin: 0;
          height: 100%;
          width: 100%;
        }
      </style>
      <link rel="stylesheet" href="https://js.arcgis.com/4.19esri/themes/light/main.css">
      <script src="https://js.arcgis.com/4.19"></script>
      <script>
        require([
    
          "esri/config",
    
          "esri/Map",
          "esri/MapView",
    
          "esri/WebScene",
          "esri/views/SceneView",
          "esri/widgets/Legend"
    
        ], function(esriConfig,WebScene, SceneView, Legend) {
    
         esriConfig.apiKey = "YOUR-API-KEY";
    
          const map = new Map ({
            basemap: "arcgis-topographic"
          });
    
          const view = new MapView({
              map: map,
              center: [-118.805, 34.027], // Longitude, latitude
              zoom: 13, // Zoom level
              container: "viewDiv" // Div element
            });
    
          const webscene = new WebScene({
            portalItem: {
              id: "579f97b2f3b94d4a8e48a5f140a6639b"
            }
          });
    
          const view = new SceneView({
            container: "viewDiv",
            map: webscene
          });
    
          const legend = new Legend ({
            view:view
          });
    
          view.ui.add(legend, "top-right");
    
        });
      </script>
    </head>
    <body>
      <div id="viewDiv"></div>
    </body>
    </html>

Load the web scene

You can use the portal item ID to create a WebScene. The web scene will be passed to the view.

  1. Delete the code that creates the Map and the MapView.

    Remove lineRemove lineRemove lineRemove lineRemove lineRemove lineRemove lineRemove lineRemove lineRemove line
                                                                      
    <html>
    <head>
      <meta charset="utf-8">
      <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no">
      <title>ArcGIS API for JavaScript Tutorials: Display a web scene</title>
      <style>
        html, body, #viewDiv {
          padding: 0;
          margin: 0;
          height: 100%;
          width: 100%;
        }
      </style>
      <link rel="stylesheet" href="https://js.arcgis.com/4.19esri/themes/light/main.css">
      <script src="https://js.arcgis.com/4.19"></script>
      <script>
        require([
    
          "esri/config",
    
          "esri/Map",
          "esri/MapView",
    
          "esri/WebScene",
          "esri/views/SceneView",
          "esri/widgets/Legend"
    
        ], function(esriConfig,WebScene, SceneView, Legend) {
    
         esriConfig.apiKey = "YOUR-API-KEY";
    
          const map = new Map ({
            basemap: "arcgis-topographic"
          });
    
          const view = new MapView({
              map: map,
              center: [-118.805, 34.027], // Longitude, latitude
              zoom: 13, // Zoom level
              container: "viewDiv" // Div element
            });
    
          const webscene = new WebScene({
            portalItem: {
              id: "579f97b2f3b94d4a8e48a5f140a6639b"
            }
          });
    
          const view = new SceneView({
            container: "viewDiv",
            map: webscene
          });
    
          const legend = new Legend ({
            view:view
          });
    
          view.ui.add(legend, "top-right");
    
        });
      </script>
    </head>
    <body>
      <div id="viewDiv"></div>
    </body>
    </html>
  2. Create a WebScene. Load the web scene using its portal item ID.

    Add line.Add line.Add line.Add line.Add line.
                                                                      
    <html>
    <head>
      <meta charset="utf-8">
      <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no">
      <title>ArcGIS API for JavaScript Tutorials: Display a web scene</title>
      <style>
        html, body, #viewDiv {
          padding: 0;
          margin: 0;
          height: 100%;
          width: 100%;
        }
      </style>
      <link rel="stylesheet" href="https://js.arcgis.com/4.19esri/themes/light/main.css">
      <script src="https://js.arcgis.com/4.19"></script>
      <script>
        require([
    
          "esri/config",
    
          "esri/Map",
          "esri/MapView",
    
          "esri/WebScene",
          "esri/views/SceneView",
          "esri/widgets/Legend"
    
        ], function(esriConfig,WebScene, SceneView, Legend) {
    
         esriConfig.apiKey = "YOUR-API-KEY";
    
          const map = new Map ({
            basemap: "arcgis-topographic"
          });
    
          const view = new MapView({
              map: map,
              center: [-118.805, 34.027], // Longitude, latitude
              zoom: 13, // Zoom level
              container: "viewDiv" // Div element
            });
    
          const webscene = new WebScene({
            portalItem: {
              id: "579f97b2f3b94d4a8e48a5f140a6639b"
            }
          });
    
          const view = new SceneView({
            container: "viewDiv",
            map: webscene
          });
    
          const legend = new Legend ({
            view:view
          });
    
          view.ui.add(legend, "top-right");
    
        });
      </script>
    </head>
    <body>
      <div id="viewDiv"></div>
    </body>
    </html>

Create a scene view

  1. Create a SceneView and set the container and map properties.

    Add line.Add line.Add line.Add line.
                                                                      
    <html>
    <head>
      <meta charset="utf-8">
      <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no">
      <title>ArcGIS API for JavaScript Tutorials: Display a web scene</title>
      <style>
        html, body, #viewDiv {
          padding: 0;
          margin: 0;
          height: 100%;
          width: 100%;
        }
      </style>
      <link rel="stylesheet" href="https://js.arcgis.com/4.19esri/themes/light/main.css">
      <script src="https://js.arcgis.com/4.19"></script>
      <script>
        require([
    
          "esri/config",
    
          "esri/Map",
          "esri/MapView",
    
          "esri/WebScene",
          "esri/views/SceneView",
          "esri/widgets/Legend"
    
        ], function(esriConfig,WebScene, SceneView, Legend) {
    
         esriConfig.apiKey = "YOUR-API-KEY";
    
          const map = new Map ({
            basemap: "arcgis-topographic"
          });
    
          const view = new MapView({
              map: map,
              center: [-118.805, 34.027], // Longitude, latitude
              zoom: 13, // Zoom level
              container: "viewDiv" // Div element
            });
    
          const webscene = new WebScene({
            portalItem: {
              id: "579f97b2f3b94d4a8e48a5f140a6639b"
            }
          });
    
          const view = new SceneView({
            container: "viewDiv",
            map: webscene
          });
    
          const legend = new Legend ({
            view:view
          });
    
          view.ui.add(legend, "top-right");
    
        });
      </script>
    </head>
    <body>
      <div id="viewDiv"></div>
    </body>
    </html>
  2. At the top-right, click Run to verify that the web map has been successfully loaded and displays.

Add widgets

Use the Legend widget to add more context to the application. The Legend widget displays labels and symbols for layers visible in the view.

  1. Create a Legend to display feature information. Add the legend to the top-right of the view.

    Add line.Add line.Add line.Add line.Add line.
                                                                      
    <html>
    <head>
      <meta charset="utf-8">
      <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no">
      <title>ArcGIS API for JavaScript Tutorials: Display a web scene</title>
      <style>
        html, body, #viewDiv {
          padding: 0;
          margin: 0;
          height: 100%;
          width: 100%;
        }
      </style>
      <link rel="stylesheet" href="https://js.arcgis.com/4.19esri/themes/light/main.css">
      <script src="https://js.arcgis.com/4.19"></script>
      <script>
        require([
    
          "esri/config",
    
          "esri/Map",
          "esri/MapView",
    
          "esri/WebScene",
          "esri/views/SceneView",
          "esri/widgets/Legend"
    
        ], function(esriConfig,WebScene, SceneView, Legend) {
    
         esriConfig.apiKey = "YOUR-API-KEY";
    
          const map = new Map ({
            basemap: "arcgis-topographic"
          });
    
          const view = new MapView({
              map: map,
              center: [-118.805, 34.027], // Longitude, latitude
              zoom: 13, // Zoom level
              container: "viewDiv" // Div element
            });
    
          const webscene = new WebScene({
            portalItem: {
              id: "579f97b2f3b94d4a8e48a5f140a6639b"
            }
          });
    
          const view = new SceneView({
            container: "viewDiv",
            map: webscene
          });
    
          const legend = new Legend ({
            view:view
          });
    
          view.ui.add(legend, "top-right");
    
        });
      </script>
    </head>
    <body>
      <div id="viewDiv"></div>
    </body>
    </html>

Run the app

In CodePen, run your code to display the map.

What's next?

Learn how to use additional API features and ArcGIS services in these tutorials: