Display a map

Learn how to create and display a map with a basemap layer.

A map contains layers of geographic data. A map contains a basemap layer and, optionally, one or more data layers. You can display a specific area of a map using a map view and setting the location and zoom level.

This tutorial shows you how to create and display a map of the Santa Monica Mountains in California using the topographic basemap layer.

The map and code in this tutorial will be used as the starting point for other 2D tutorials.

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. Go to CodePen to create a new pen for your mapping application.

Add HTML

Define an HTML page to create a map that is the full width and height of the web browser window.

  1. In CodePen > HTML, add HTML and CSS to create a page with a viewDiv element. The viewDiv is the element displays the map and its CSS resets any browser settings so it can consume the full width and height of the browser.

    The <!DOCTYPE html> tag is not required in CodePen. If you are using a different editor or running the page on a local server, be sure to add this tag to the top of your HTML page.

    Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.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 map</title>
    
        <style>
          html,
          body,
          #viewDiv {
            padding: 0;
            margin: 0;
            height: 100%;
            width: 100%;
          }
        </style>
    
        <link rel="stylesheet" href="https://js.arcgis.com/4.18/esri/themes/light/main.css">
        <script src="https://js.arcgis.com/4.18/"></script>
    
        <script>
          require(["esri/config","esri/Map", "esri/views/MapView"], function (esriConfig,Map, MapView) {
    
            esriConfig.apiKey = "YOUR-API-KEY";
    
            const map = new Map({
              basemap: "arcgis-topographic" // Basemap layer service
            });
    
            const view = new MapView({
              map: map,
              center: [-118.805, 34.027], // Longitude, latitude
              zoom: 13, // Zoom level
              container: "viewDiv" // Div element
            });
    
          });
        </script>
    
      </head>
      <body>
        <div id="viewDiv"></div>
      </body>
    </html>

Reference the API

  1. In the <head> tag, add references to the CSS file and JS library.

    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 map</title>
    
        <style>
          html,
          body,
          #viewDiv {
            padding: 0;
            margin: 0;
            height: 100%;
            width: 100%;
          }
        </style>
    
        <link rel="stylesheet" href="https://js.arcgis.com/4.18/esri/themes/light/main.css">
        <script src="https://js.arcgis.com/4.18/"></script>
    
        <script>
          require(["esri/config","esri/Map", "esri/views/MapView"], function (esriConfig,Map, MapView) {
    
            esriConfig.apiKey = "YOUR-API-KEY";
    
            const map = new Map({
              basemap: "arcgis-topographic" // Basemap layer service
            });
    
            const view = new MapView({
              map: map,
              center: [-118.805, 34.027], // Longitude, latitude
              zoom: 13, // Zoom level
              container: "viewDiv" // Div element
            });
    
          });
        </script>
    
      </head>
      <body>
        <div id="viewDiv"></div>
      </body>
    </html>

Add modules

The ArcGIS JS API contains AMD modules. Reference the Map and MapView modules in the require statement.

  1. In the <head> tag, add a <script> tag and an AMD require statement to load the Map and MapView modules.

    You can also add the JavaScript code to the CodePen > JS panel instead of the HTML panel. If you do so, remove the <script> tag.

    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 map</title>
    
        <style>
          html,
          body,
          #viewDiv {
            padding: 0;
            margin: 0;
            height: 100%;
            width: 100%;
          }
        </style>
    
        <link rel="stylesheet" href="https://js.arcgis.com/4.18/esri/themes/light/main.css">
        <script src="https://js.arcgis.com/4.18/"></script>
    
        <script>
          require(["esri/config","esri/Map", "esri/views/MapView"], function (esriConfig,Map, MapView) {
    
            esriConfig.apiKey = "YOUR-API-KEY";
    
            const map = new Map({
              basemap: "arcgis-topographic" // Basemap layer service
            });
    
            const view = new MapView({
              map: map,
              center: [-118.805, 34.027], // Longitude, latitude
              zoom: 13, // Zoom level
              container: "viewDiv" // Div element
            });
    
          });
        </script>
    
      </head>
      <body>
        <div id="viewDiv"></div>
      </body>
    </html>

Get an API key

An API key is required to access ArcGIS services.

  1. Go to your developer dashboard to get an API key.
  2. Copy the key as it will be used in the next step.

Create a map

Use a Map to set the basemap layer and apply your API key.

  1. Go back to CodePen.

  2. In the require statement, create a new Map and set the basemap property to arcgis-topographic. To enable access to the Basemap layer service, set the apiKey() property of the Map.

    A map can contain data layers and a basemap layer. In order to access a basemap layer from the Basemap layer service, you need an API key. You can set the key from the previous step when you create the Map.

    Learn more about how a map and a map view work in Maps (2D) in the Mapping and location services guide.

    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 map</title>
    
        <style>
          html,
          body,
          #viewDiv {
            padding: 0;
            margin: 0;
            height: 100%;
            width: 100%;
          }
        </style>
    
        <link rel="stylesheet" href="https://js.arcgis.com/4.18/esri/themes/light/main.css">
        <script src="https://js.arcgis.com/4.18/"></script>
    
        <script>
          require(["esri/config","esri/Map", "esri/views/MapView"], function (esriConfig,Map, MapView) {
    
            esriConfig.apiKey = "YOUR-API-KEY";
    
            const map = new Map({
              basemap: "arcgis-topographic" // Basemap layer service
            });
    
            const view = new MapView({
              map: map,
              center: [-118.805, 34.027], // Longitude, latitude
              zoom: 13, // Zoom level
              container: "viewDiv" // Div element
            });
    
          });
        </script>
    
      </head>
      <body>
        <div id="viewDiv"></div>
      </body>
    </html>

Create a map view

Use a MapView class to set the location of the map to display.

  1. Create a MapView and set the map property. To center the map view, set the center property to -118.80500,34.02700 and the zoom property to 13. Set the container property to viewDiv to display the contents of the map.

    The MapView displays the contents of a map. The center and zoom properties determine the location of the map and the zoom level at which it is displayed when it loads.

    The zoom property sets the zoom level of the map. The values typically range from 0-20: 0 is the farthest from the earth's surface, and 20 is the closest. Some basemap layers can support additional zoom levels up to 23.

    The MapView also supports a number of touch events such as click and double-click. You can use these events to change the position of the map or to find features in layers.

    Learn more about how a map and a map view work in Maps (2D) in the Mapping APIs and location services guide.

    Add line.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 map</title>
    
        <style>
          html,
          body,
          #viewDiv {
            padding: 0;
            margin: 0;
            height: 100%;
            width: 100%;
          }
        </style>
    
        <link rel="stylesheet" href="https://js.arcgis.com/4.18/esri/themes/light/main.css">
        <script src="https://js.arcgis.com/4.18/"></script>
    
        <script>
          require(["esri/config","esri/Map", "esri/views/MapView"], function (esriConfig,Map, MapView) {
    
            esriConfig.apiKey = "YOUR-API-KEY";
    
            const map = new Map({
              basemap: "arcgis-topographic" // Basemap layer service
            });
    
            const view = new MapView({
              map: map,
              center: [-118.805, 34.027], // Longitude, latitude
              zoom: 13, // Zoom level
              container: "viewDiv" // Div element
            });
    
          });
        </script>
    
      </head>
      <body>
        <div id="viewDiv"></div>
      </body>
    </html>

Run the app

In CodePen, run your code to display the map.

The map should display the topographic basemap 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: