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.

    Use dark colors for code blocks
                                                
    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.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    <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 map</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

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

    Expand
    Use dark colors for code blocks
                                                
    Add line.Add line.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
        <style>
          html,
          body,
          #viewDiv {
            padding: 0;
            margin: 0;
            height: 100%;
            width: 100%;
          }
        </style>
    
        <link rel="stylesheet" href="https://js.arcgis.com/4.26/esri/themes/light/main.css">
        <script src="https://js.arcgis.com/4.26/"></script>
    
    Expand

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/Map" 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 Introduction to Tooling guide topic.

  1. In the <head> tag, add a <script> tag and a 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.

    Expand
    Use dark colors for code blocks
                                                
    Add line.Add line.Add line.Add line.Add line.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
        <link rel="stylesheet" href="https://js.arcgis.com/4.26/esri/themes/light/main.css">
        <script src="https://js.arcgis.com/4.26/"></script>
    
        <script>
          require(["esri/config", "esri/Map", "esri/views/MapView"], function(esriConfig, Map, MapView) {
    
          });
        </script>
    
    Expand

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.

    Expand
    Use dark colors for code blocks
                                                
    Add line.Add line.Add line.Add line.Add line.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
        <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
            });
    
    Expand

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.

    Expand
    Use dark colors for code blocks
                                                
    Add line.Add line.Add line.Add line.Add line.Add line.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
            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
            });
    
    Expand

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:

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.