Skip to content

In this tutorial, you will learn how to create and display a map with a basemap .

Pan and zoom to explore the map.

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

This tutorial shows you how to create and display a street map of Santa Monica, California.

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

Prerequisites

Steps

Create a new pen

  1. Go to CodePen to create a new pen for your mapping application.

Add basic HTML

Define a basic HTML page.

  1. In CodePen > HTML, add HTML to create a basic page.
    <html>
    <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
    <title>ArcGIS Maps SDK for JavaScript Tutorials: Display a map</title>
    </head>
    <body>
    </body>
    </html>

Add CSS

  1. In CodePen > HTML, add CSS to set the map to use the full width and height of the browser.
    6 collapsed lines
    <html>
    <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
    <title>ArcGIS Maps SDK for JavaScript Tutorials: Display a map</title>
    <style>
    html,
    body {
    height: 100%;
    margin: 0;
    }
    </style>
    12 collapsed lines
    </head>
    <body>
    <script type="module">
    </script>
    </body>
    </html>

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(s) :
    • Privileges
      • Location services > Basemaps
  2. In CodePen, set the apiKey property on the global esriConfig variable to your access token.
    <!-- The esriConfig variable must be defined before adding the other esri libraries -->
    <script>
    var esriConfig = {
    apiKey: "YOUR_ACCESS_TOKEN",
    };
    </script>

To learn about other ways to get an access token, go to Types of authentication.

Add the ArcGIS Maps SDK for JavaScript script tag

  1. In the <head>, add the <script> tag for the JavaScript Maps SDK.

    20 collapsed lines
    <html>
    <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
    <title>ArcGIS Maps SDK for JavaScript Tutorials: Display a map</title>
    <style>
    html,
    body {
    height: 100%;
    margin: 0;
    }
    </style>
    <script>
    var esriConfig = {
    apiKey: "YOUR_ACCESS_TOKEN",
    };
    </script>
    <!-- Load the ArcGIS Maps SDK for JavaScript from CDN -->
    <script type="module" src="https://js.arcgis.com/5.0/"></script>
    12 collapsed lines
    </head>
    <body>
    <script type="module">
    </script>
    </body>
    </html>

Add a map

Use the Map component to set the basemap , location and zoom level . There are many more basemaps to choose from, to learn more visit the ArcGIS Location Services introduction to Basemaps.

  1. In the <body> tag, add the <arcgis-map> component and specify the center and zoom attributes.

    25 collapsed lines
    <html>
    <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
    <title>ArcGIS Maps SDK for JavaScript Tutorials: Display a map</title>
    <style>
    html,
    body {
    height: 100%;
    margin: 0;
    }
    </style>
    <script>
    var esriConfig = {
    apiKey: "YOUR_ACCESS_TOKEN",
    };
    </script>
    <!-- Load the ArcGIS Maps SDK for JavaScript from CDN -->
    <script type="module" src="https://js.arcgis.com/5.0/"></script>
    </head>
    <body>
    <arcgis-map basemap="arcgis/community" center="-118.49178, 34.01185" zoom="15">
    </arcgis-map>
    </body>
    2 collapsed lines
    </html>
  2. In the <arcgis-map> component, add an <arcgis-zoom> component, which enables users to zoom in and out without a mouse wheel.

    27 collapsed lines
    <html>
    <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
    <title>ArcGIS Maps SDK for JavaScript Tutorials: Display a map</title>
    <style>
    html,
    body {
    height: 100%;
    margin: 0;
    }
    </style>
    <script>
    var esriConfig = {
    apiKey: "YOUR_ACCESS_TOKEN",
    };
    </script>
    <!-- Load the ArcGIS Maps SDK for JavaScript from CDN -->
    <script type="module" src="https://js.arcgis.com/5.0/"></script>
    </head>
    <body>
    <arcgis-map basemap="arcgis/community" center="-118.49178, 34.01185" zoom="15">
    <arcgis-zoom slot="top-left"></arcgis-zoom>
    </arcgis-map>
    4 collapsed lines
    </body>
    </html>
  3. Create a <script> block, set the script’s type attribute to module. Then use document.querySelector() to get a reference to the Map component and use the viewOnReady method to determine when it is ready.

    33 collapsed lines
    <html>
    <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
    <title>ArcGIS Maps SDK for JavaScript Tutorials: Display a map</title>
    <style>
    html,
    body {
    height: 100%;
    margin: 0;
    }
    </style>
    <script>
    var esriConfig = {
    apiKey: "YOUR_ACCESS_TOKEN",
    };
    </script>
    <!-- Load the ArcGIS Maps SDK for JavaScript from CDN -->
    <script type="module" src="https://js.arcgis.com/5.0/"></script>
    </head>
    <body>
    <arcgis-map basemap="arcgis/community" center="-118.49178, 34.01185" zoom="15">
    <arcgis-zoom slot="top-left"></arcgis-zoom>
    </arcgis-map>
    <script type="module">
    // Get a reference to the map component
    const viewElement = document.querySelector("arcgis-map");
    // Wait for when the component is ready
    await viewElement.viewOnReady();
    console.log("The map is ready.");
    </script>
    4 collapsed lines
    </body>
    </html>

Run the app

In CodePen, run your code to display the map.

The map should display the streets basemap for an area of Santa Monica in California.

What’s next?

Learn how to use additional SDK features and ArcGIS services in these tutorials: