Intro to SceneView - Create a 3D map

Loading...

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

This tutorial will walk you through how to create a simple map in a 3D SceneView.

1. Reference the ArcGIS API for JavaScript

First, set up a basic HTML document similar to the following example:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no">
<title>Intro to SceneView - Create a 3D map</title>
</head>
</html>

Inside the <head> tags, reference the ArcGIS API for JavaScript using <script> and <link> tags:

<link rel="stylesheet" href="https://js.arcgis.com/4.9/esri/css/main.css">
<script src="https://js.arcgis.com/4.9/"></script>

The <script> tag loads the ArcGIS API for JavaScript from a CDN. When new versions of the API are released, update the version number to match the newly released version.

The <link> tag references the main.css style sheet which contains styles specific to Esri widgets and components.

2. Load the modules

Use a second <script> tag to load specific modules from the API. Load the following modules using the syntax in the snippet below:

  • esri/Map - loads code specific to creating a map
  • esri/views/SceneView - loads code that allows for viewing the map in 3D
<script>
require([
  "esri/Map",
  "esri/views/SceneView"
], function(Map, SceneView) {
  // Code to create the map and view will go here
});
</script>

Putting JavaScript inside a script tag is useful when building simple pages or experimenting, but not suitable for larger applications. When building a larger application, all JavaScript should be in separate .js files.

The global require() function (provided by Dojo) is used to load modules. Esri's JavaScript API is built on top of Dojo to take advantage of Dojo's modularized code base and its ability to reconcile cross-browser differences. For more information on the relationship between Dojo and the JavaScript API, see Working with Dojo and Why Dojo.

3. Create the map

A new map is created using Map, which is a reference to the Map class that was loaded from the esri/Map module. You can specify map properties, such as basemap and 'ground, by passing an object to the Map constructor.

require([
  "esri/Map",
  "esri/views/SceneView"
], function(Map, SceneView) {
  var map = new Map({
    basemap: "streets",
    ground: "world-elevation"
  });
});

Additional basemap options are: satellite, hybrid, topo, gray, dark-gray, oceans, osm, national-geographic. Use alternate basemaps by modifying the basemap option in the sandbox. View the Map class for more details on additional map options.

The 'ground' property of 'Map' specifies the surface properties for the map. It is only relevant when adding the map to a 3D SceneView. The string 'world-elevation' specifies an instance of ground using the World Elevation Service.

4. Create a 3D view

Views reference nodes that serve as containers in HTML files, allowing users to view the map inside an HTML page. Create a new SceneView and set its properties by passing an object to its constructor:

require([
  "esri/Map",
  "esri/views/SceneView"
], function(Map, SceneView) {
  var map = new Map({
    basemap: "streets",
    ground: "world-elevation"
  });
  var view = new SceneView({
    container: "viewDiv",  // Reference to the DOM node that will contain the view
    map: map  // References the map object created in step 3
  });
});

In this snippet, we set the container property to the name of the DOM node that will hold the map. The map property references the map object we created in the previous step. See the SceneView documentation for additional properties you may set on the view, including center and scale, which may be used to define the initial extent of the view.

There are two types of views available: MapView (used for viewing 2D maps) and SceneView (used for viewing 3D maps). Click here to learn more about creating maps with 2D views.

5. Define the page content

Now the JavaScript needed to create a map and a view is complete! The next step is to add the associated HTML for viewing the map. For this example, the HTML is very simple: add a <body> tag, which defines what is visible in the browser, and a single <div> element inside the body where the view will be created.

<body>
  <div id="viewDiv"></div>
</body>

The <div> has an id of "viewDiv" to match the id that is passed to the SceneView's container property in the constructor.

6. Style the page

Style the content of the page using <style> tags inside the <head>. To make the map fill the browser window, add the following CSS inside the page's <style>:

<style>
  html, body, #viewDiv {
    padding: 0;
    margin: 0;
    height: 100%;
    width: 100%;
  }
</style>

You've now built your first web application in 3D using the ArcGIS API for JavaScript 4.0! Your final HTML code should look like the following:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no">
<title>Intro to SceneView - Create a 3D map</title>
<style>
  html, body, #viewDiv {
    padding: 0;
    margin: 0;
    height: 100%;
    width: 100%;
  }
</style>
<link rel="stylesheet" href="https://js.arcgis.com/4.9/esri/css/main.css">
<script src="https://js.arcgis.com/4.9/"></script>
<script>
require([
  "esri/Map",
  "esri/views/SceneView"
], function(Map, SceneView){
  var map = new Map({
    basemap: "streets",
    ground: "world-elevation"
  });
  var view = new SceneView({
    container: "viewDiv",     // Reference to the scene div created in step 5
    map: map,                 // Reference to the map object created before the scene
    scale: 50000000,          // Sets the initial scale to 1:50,000,000
    center: [-101.17, 21.78]  // Sets the center point of view with lon/lat
  });
});
</script>
</head>
<body>
  <div id="viewDiv"></div>
</body>
</html>

Sample search results

TitleSample
Loading...