Learn how to create and display a scene with a basemap layer and elevation.
A scene contains layers of geographic data that are displayed in 3D. Scenes allow you to display real-world visualizations of landscapes, objects, buildings, and other 3D objects. You display a scene using a scene view to display a basemap layer and data layers, and also to control the center location, tilt, and angle of the view.
In this tutorial, you will create and display a scene of the Santa Monica Mountains in California using the topographic basemap layer and the world elevation layer. The scene and code will be used as the starting point for other 3D tutorials.
Prerequisites
Steps
Create a new pen
- Go to CodePen to create a new pen for your mapping application.
Add HTML
Define an HTML page to create a scene that is the full width and height of the browser window.
-
In CodePen > HTML, add HTML and CSS to create a page with a
view
element for the scene.Div The HTML will create a scene that is the full width and height of the page. The
view
is the element displays the scene and its CSS resets any browser settings so it can consume the full width and height of the browser.Div Use dark colors for code blocks <html> <head> <meta charset="utf-8"> <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no"> <title>ArcGIS Maps SDK for JavaScript Tutorials: Display a scene</title> <style> html, body, #viewDiv { padding: 0; margin: 0; height: 100%; width: 100%; } </style> </head> <body> <div id="viewDiv"></div> </body> </html>
Reference the API
- In the
<head
tag, add references to the CSS file and JS library.> Use dark colors for code blocks <html> <head> <meta charset="utf-8"> <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no"> <title>ArcGIS Maps SDK for JavaScript Tutorials: Display a scene</title> <style> html, body, #viewDiv { padding: 0; margin: 0; height: 100%; width: 100%; } </style> <link rel="stylesheet" href="https://js.arcgis.com/4.30/esri/themes/light/main.css"> <script src="https://js.arcgis.com/4.30/"></script> </head> <body> <div id="viewDiv"></div> </body> </html>
Add modules
The ArcGIS Maps SDK for JavaScript is available as AMD modules and ES modules, but this tutorial is based on AMD. The AMD require
function uses references to determine which modules will be loaded – for example, you can specify "esri/
for loading the Map module. After the modules are loaded, they are passed as parameters (e.g. Map
) to the callback function where they can be used in your application. It is important to keep the module references and callback parameters in the same order. For more information on the different types of modules, visit the Overview Guide page.
- Add a
<script
tag and a> require
statement to load theMap
andScene
modules. You can also add the JavaScript code to the CodePen > JS panel instead of the HTML panel. If you do so, remove theView <script
tag.> Use dark colors for code blocks <html> <head> <meta charset="utf-8"> <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no"> <title>ArcGIS Maps SDK for JavaScript Tutorials: Display a scene</title> <style> html, body, #viewDiv { padding: 0; margin: 0; height: 100%; width: 100%; } </style> <link rel="stylesheet" href="https://js.arcgis.com/4.30/esri/themes/light/main.css"> <script src="https://js.arcgis.com/4.30/"></script> <script> require([ "esri/config", "esri/Map", "esri/views/SceneView" ], function(esriConfig, Map, SceneView) { }); </script> </head> <body> <div id="viewDiv"></div> </body> </html>
Get an access token
You need an access token with the correct privileges to access the location services used in this tutorial.
- Go to the Create an API key tutorial and create an API key with the following privilege:
- Privileges
- Location services > Basemaps
- Privileges
- Copy the API key as it will be used in the next step.
To learn about other ways to get an access token, go to Types of authentication.
Create a scene
Use a Map
to define the basemap layer and elevation surface. Scenes use the information in the elevation layer to determine the ground
(surface) height that will be rendered on the map.
-
Go back to CodePen.
-
At the beginning of the main function, set the
api
property to use your access token.Key Use dark colors for code blocks <script> require([ "esri/config", "esri/Map", "esri/views/SceneView" ], function(esriConfig, Map, SceneView) { esriConfig.apiKey = "YOUR_ACCESS_TOKEN";
-
Create a
Map
and set thebasemap
property toarcgis/topographic
and theground
property toworld-elevation
.Use dark colors for code blocks esriConfig.apiKey = "YOUR_ACCESS_TOKEN"; const map = new Map({ basemap: "arcgis/topographic", // basemap styles service ground: "world-elevation", //Elevation service });
Create a scene view
Use a Scene
class to set the map
and layers to draw, as well as to define the camera position. The camera
is the point from which the visible extent of the Scene
is determined. The tilt
property refers to the number of degrees from the surface that the camera is pointed.
- Create a
Scene
. Set theView container
,map
, andcamera
properties.Use dark colors for code blocks const map = new Map({ basemap: "arcgis/topographic", // basemap styles service ground: "world-elevation", //Elevation service }); const view = new SceneView({ container: "viewDiv", map: map, camera: { position: { x: -118.808, //Longitude y: 33.961, //Latitude z: 2000 //Meters }, tilt: 75 } });
Run the app
In CodePen, run your code to display the map.
The scene view should display the topographic basemap layer and elevation layer for an area of the Santa Monica Mountains in California.
What's next?
Learn how to use additional API features and ArcGIS services in these tutorials: