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

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.

    Use dark colors for code blocks
    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
    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.30/esri/themes/light/main.css">
        <script src="https://js.arcgis.com/4.30/"></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 Overview 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
    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.30/esri/themes/light/main.css">
        <script src="https://js.arcgis.com/4.30/"></script>
    
        <script>
          require(["esri/config", "esri/Map", "esri/views/MapView"], function(esriConfig, Map, MapView) {
    
          });
        </script>
    
    Expand

Get an access token

You need an access token with the correct privileges to access the location services used in this tutorial.

  1. Go to the Create an API key tutorial and create an API key with the following privilege:
    • Privileges
      • Location services > Basemaps
  2. 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 map

Use a Map to set the basemap layer and apply your access token.

  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 styles service, set the apiKey property of the Map.

    Expand
    Use dark colors for code blocks
    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_ACCESS_TOKEN";
    
            const map = new Map({
              basemap: "arcgis/topographic" // basemap styles 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.805, 34.027 and the zoom property to 13. Set the container property to viewDiv to display the contents of the map.

    Expand
    Use dark colors for code blocks
    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_ACCESS_TOKEN";
    
            const map = new Map({
              basemap: "arcgis/topographic" // basemap styles 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.