Display a web map

Learn how to display a map from a web map stored in ArcGIS.

A web map is a map stored as an item in ArcGIS Online. A web map item contains all of the configuration settings for the map (in JSON format) such as the basemap layer, data layers, layer styles, and pop-up settings. Applications can access and display a web map using its item ID.

In this tutorial, you will load and display a pre-configured web map stored in ArcGIS Online.

Prerequisites

Steps

Create a new pen

  1. To get started, either complete the Display a map tutorial or .

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 esriConfig.apiKey to your API key..
    Use dark colors for code blocks
    1
    2
    3
    4
    esriConfig.apiKey = "YOUR_ACCESS_TOKEN";
    const map = new Map({
      basemap: "arcgis/topographic" // basemap styles service
    });

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

Add modules

  1. In the require statement, delete the Map module. Add the WebMap, ScaleBar, and Legend modules.

    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
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
      <script>
        require([
          "esri/config",
          "esri/Map",
          "esri/WebMap",
          "esri/views/MapView",
          "esri/widgets/ScaleBar",
          "esri/widgets/Legend"
        ], function(esriConfig, WebMap, MapView, ScaleBar, Legend) {
    
    Expand

Load the web map

You can use the portal item ID to create a WebMap. The web map will be passed to the view.

  1. Delete the code that creates 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
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
        esriConfig.apiKey = "YOUR_ACCESS_TOKEN";
    
         const map = new Map ({
           basemap: "arcgis/topographic" // basemap styles service
         });
    
    
    Expand
  2. Create a WebMap. Set the portalItem ID to 41281c51f9de45edaf1c8ed44bb10e30.

    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
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
        esriConfig.apiKey = "YOUR_ACCESS_TOKEN";
    
          const webmap = new WebMap({
            portalItem: {
              id: "41281c51f9de45edaf1c8ed44bb10e30"
            }
          });
    
    
    Expand
  3. Update the map property in the MapView with the webmap element. Remove the center and zoom properties, since the web map provides positioning information.

    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
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
          const webmap = new WebMap({
            portalItem: {
              id: "41281c51f9de45edaf1c8ed44bb10e30"
            }
          });
    
          const view = new MapView({
            container: "viewDiv",
    
            map: map,
            center: [-118.80500,34.02700],
            zoom: 13,
    
            map: webmap
    
          });
    
    Expand
  4. At the top-right, click Run to verify that the web map has been successfully loaded and displays.

Add widgets

Use the Legend and ScaleBar widgets to add more context to the application. The Legend widget displays labels and symbols for layers visible in the view. The ScaleBar can display units in either metric or imperial values.

  1. Create a ScaleBar. Add the scalebar to the bottom-left of the view.

    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
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
          const view = new MapView({
            container: "viewDiv",
    
            map: webmap
    
          });
    
          const scalebar = new ScaleBar({
            view: view
          });
    
          view.ui.add(scalebar, "bottom-left");
    
    
    Expand
  2. Create a Legend to display feature information. Add the legend to the top-right of the view.

    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
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
          const scalebar = new ScaleBar({
            view: view
          });
    
          view.ui.add(scalebar, "bottom-left");
    
        const legend = new Legend ({
            view: view
          });
          view.ui.add(legend, "top-right");
    
    Expand

Run the App

In CodePen, run your code to display the map.

You should now see a scale bar displaying scale (in miles), along with a legend that displays layer information contained in the web map.

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.