Display a map

Learn how to display a map with a basemap layer using OpenLayers.

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 by using a map view and setting the location and zoom level.

In this tutorial you will use OpenLayers to display a map of the Santa Monica Mountains in California using the topographic basemap layer.

This tutorial is the starting point for the other OpenLayers tutorials.

Prerequisites

You need an ArcGIS Developer or ArcGIS Online account to access the developer dashboard and create an API key.

Steps

Create a new pen

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

Add HTML

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

  1. In CodePen > HTML, add HTML and CSS to create a page with a div element called map.

    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
    45
    46
    47
    48
    49
    50
    51
    52
    <html>
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no" />
        <title>OpenLayers Tutorials: Display a map</title>
    
        <style>
          html,
          body,
          #map {
            padding: 0;
            margin: 0;
            height: 100%;
            width: 100%;
            font-family: Arial, Helvetica, sans-serif;
            font-size: 14px;
            color: #323232;
          }
        </style>
    
      </head>
      <body>
        <div id="map"></div>
    
      </body>
    </html>

Load the libraries

You will need add <script> and <link> tags to load the JavaScript and CSS files for OpenLayers and ol-mapbox-style.

  1. In the <head> element, add references to the OpenLayers CSS and JavaScript 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
    45
    46
    47
    48
    49
    50
    51
    52
        <style>
          html,
          body,
          #map {
            padding: 0;
            margin: 0;
            height: 100%;
            width: 100%;
            font-family: Arial, Helvetica, sans-serif;
            font-size: 14px;
            color: #323232;
          }
        </style>
    
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/ol@v8.2.0/ol.css" type="text/css" />
        <script src="https://cdn.jsdelivr.net/npm/ol@v8.2.0/dist/ol.js"></script>
    
    
    Expand
  2. Add a reference to the ol-mapbox-style library, which is required to load and style a vector tile layer using a vector tile style in OpenLayers.

    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
    45
    46
    47
    48
    49
    50
    51
    52
        <script src="https://cdn.jsdelivr.net/npm/ol-mapbox-style@10.6.0/dist/olms.js"></script>
    
    Expand

Create a map and view

Use the Map and View classes to create an OpenLayers map.

  1. Add a <script> element in the <body> element.

    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
    45
    46
    47
    48
    49
    50
    51
    52
      </head>
      <body>
        <div id="map"></div>
    
        <script>
    
        </script>
    
    Expand
  2. Use the Map class to create the map with options to control its display and behavior. Set the target property to "map", which is the id of the div element.

    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
    45
    46
    47
    48
    49
    50
    51
    52
        <script>
    
          const map = new ol.Map({ target: "map" });
    
        </script>
    
    Expand
  3. Create a View, and use setView to apply it to the map. To center the map view, set the center property to -118.80500,34.02700 and the zoom property to 13.

    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
    45
    46
    47
    48
    49
    50
    51
    52
        <script>
    
          const map = new ol.Map({ target: "map" });
    
          map.setView(
            new ol.View({
              center: ol.proj.fromLonLat([-118.805, 34.027]),
              zoom: 12
            })
          );
    
        </script>
    
    Expand

Get an API key

To access location services, you need an API key or OAuth 2.0 access token. To learn how to create and scope your key, visit the Create an API key tutorial.

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

Add the basemap layer

OpenLayers does not directly support a vector basemap or a vector style file, so you will use the openlayers-mapbox-style (olms) JavaScript library to load a Mapbox style from the Basemap styles service and render it with OpenLayers.

  1. Store your API key as a variable. You will need to include this API key whenever you invoke ArcGIS services.

    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
    45
    46
    47
    48
    49
    50
    51
    52
          map.setView(
            new ol.View({
              center: ol.proj.fromLonLat([-118.805, 34.027]),
              zoom: 12
            })
          );
    
          const apiKey = "YOUR_API_KEY";
    
    Expand
  2. Create a basemapId variable and set it to arcgis/streets.

    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
    45
    46
    47
    48
    49
    50
    51
    52
          const apiKey = "YOUR_API_KEY";
    
          const basemapId = "arcgis/streets";
    
    
    Expand
  3. Construct the basemap URL based on basemapId and your API key.

    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
    45
    46
    47
    48
    49
    50
    51
    52
          const apiKey = "YOUR_API_KEY";
    
          const basemapId = "arcgis/streets";
    
          const basemapURL = `https://basemapstyles-api.arcgis.com/arcgis/rest/services/styles/v2/styles/${basemapId}?token=${apiKey}`;
    
    
    Expand
  4. Use olms to apply the basemap style to your map. The olms function takes two inputs, the map element and the URL for a Mapbox basemap style file.

    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
    45
    46
    47
    48
    49
    50
    51
    52
          const apiKey = "YOUR_API_KEY";
    
          const basemapId = "arcgis/streets";
    
          const basemapURL = `https://basemapstyles-api.arcgis.com/arcgis/rest/services/styles/v2/styles/${basemapId}?token=${apiKey}`;
    
          olms.apply(map, basemapURL);
    
    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. Pan and zoom to explore the map.

What's next?

Learn how to use additional ArcGIS location 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.