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 load and display a pre-configured web map stored in ArcGIS Online.

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. To get started, either complete the Display a map tutorial or .

Set the API key

To access ArcGIS services, you need an API key.

  1. Go to your dashboard to get an API key.

  2. In CodePen, set the apiKey to your key, so it can be used to access basemap layer and location services.

    Change line
        
    1
    2
    3
    4
    esriConfig.apiKey = "YOUR_API_KEY";
    const map = new Map({
      basemap: "arcgis-topographic" // Basemap layer service
    });
    

Add modules

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

    The ArcGIS API for JavaScript uses AMD modules. The require function is used to load modules so they can be used in the main function. It's important to keep the module references and function parameters in the same order.

    Remove lineAdd line.Add line.Add line.Change 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
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    <html>
    <head>
      <meta charset="utf-8">
      <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no">
      <title>ArcGIS API for JavaScript Tutorials: Display a Web Map</title>
      <style>
        html, body, #viewDiv {
          padding: 0;
          margin: 0;
          height: 100%;
          width: 100%;
      </style>
      <link rel="stylesheet" href="https://js.arcgis.com/4.20/esri/themes/light/main.css">
      <script src="https://js.arcgis.com/4.20/"></script>
    
      <script>
        require([
          "esri/config",
          "esri/Map",
          "esri/WebMap",
          "esri/views/MapView",
          "esri/widgets/ScaleBar",
          "esri/widgets/Legend"
        ], function(esriConfig, WebMap, MapView, ScaleBar, Legend) {
        esriConfig.apiKey = "YOUR_API_KEY";
         const map = new Map ({
           basemap: "arcgis-topographic" //Basemap layer service
          const webmap = new WebMap({
            portalItem: {
              id: "41281c51f9de45edaf1c8ed44bb10e30"
          const view = new MapView({
            container: "viewDiv",
            map: map,
            center: [-118.80500,34.02700],
            zoom: 13
            map: webmap
          const scalebar = new ScaleBar({
            view: view
          view.ui.add(scalebar, "bottom-left");
        const legend = new Legend ({
            view: view
          view.ui.add(legend, "top-right");
      </script>
    </head>
    <body>
      <div id="viewDiv"></div>
    </body>
    </html>

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.

    Remove lineRemove lineRemove 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
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    <html>
    <head>
      <meta charset="utf-8">
      <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no">
      <title>ArcGIS API for JavaScript Tutorials: Display a Web Map</title>
      <style>
        html, body, #viewDiv {
          padding: 0;
          margin: 0;
          height: 100%;
          width: 100%;
      </style>
      <link rel="stylesheet" href="https://js.arcgis.com/4.20/esri/themes/light/main.css">
      <script src="https://js.arcgis.com/4.20/"></script>
    
      <script>
        require([
          "esri/config",
          "esri/Map",
          "esri/WebMap",
          "esri/views/MapView",
          "esri/widgets/ScaleBar",
          "esri/widgets/Legend"
        ], function(esriConfig, WebMap, MapView, ScaleBar, Legend) {
        esriConfig.apiKey = "YOUR_API_KEY";
    
         const map = new Map ({
           basemap: "arcgis-topographic" //Basemap layer service
         });
    
          const webmap = new WebMap({
            portalItem: {
              id: "41281c51f9de45edaf1c8ed44bb10e30"
          const view = new MapView({
            container: "viewDiv",
            map: map,
            center: [-118.80500,34.02700],
            zoom: 13
            map: webmap
          const scalebar = new ScaleBar({
            view: view
          view.ui.add(scalebar, "bottom-left");
        const legend = new Legend ({
            view: view
          view.ui.add(legend, "top-right");
      </script>
    </head>
    <body>
      <div id="viewDiv"></div>
    </body>
    </html>
  2. Create a WebMap. Set the portalItem ID to 41281c51f9de45edaf1c8ed44bb10e30.

    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
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    <html>
    <head>
      <meta charset="utf-8">
      <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no">
      <title>ArcGIS API for JavaScript Tutorials: Display a Web Map</title>
      <style>
        html, body, #viewDiv {
          padding: 0;
          margin: 0;
          height: 100%;
          width: 100%;
      </style>
      <link rel="stylesheet" href="https://js.arcgis.com/4.20/esri/themes/light/main.css">
      <script src="https://js.arcgis.com/4.20/"></script>
    
      <script>
        require([
          "esri/config",
          "esri/Map",
          "esri/WebMap",
          "esri/views/MapView",
          "esri/widgets/ScaleBar",
          "esri/widgets/Legend"
        ], function(esriConfig, WebMap, MapView, ScaleBar, Legend) {
        esriConfig.apiKey = "YOUR_API_KEY";
    
         const map = new Map ({
           basemap: "arcgis-topographic" //Basemap layer service
          const webmap = new WebMap({
            portalItem: {
              id: "41281c51f9de45edaf1c8ed44bb10e30"
            }
          });
    
          const view = new MapView({
            container: "viewDiv",
            map: map,
            center: [-118.80500,34.02700],
            zoom: 13
            map: webmap
          const scalebar = new ScaleBar({
            view: view
          view.ui.add(scalebar, "bottom-left");
        const legend = new Legend ({
            view: view
          view.ui.add(legend, "top-right");
      </script>
    </head>
    <body>
      <div id="viewDiv"></div>
    </body>
    </html>
  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.

    Remove lineRemove lineRemove lineChange 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
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    <html>
    <head>
      <meta charset="utf-8">
      <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no">
      <title>ArcGIS API for JavaScript Tutorials: Display a Web Map</title>
      <style>
        html, body, #viewDiv {
          padding: 0;
          margin: 0;
          height: 100%;
          width: 100%;
      </style>
      <link rel="stylesheet" href="https://js.arcgis.com/4.20/esri/themes/light/main.css">
      <script src="https://js.arcgis.com/4.20/"></script>
    
      <script>
        require([
          "esri/config",
          "esri/Map",
          "esri/WebMap",
          "esri/views/MapView",
          "esri/widgets/ScaleBar",
          "esri/widgets/Legend"
        ], function(esriConfig, WebMap, MapView, ScaleBar, Legend) {
        esriConfig.apiKey = "YOUR_API_KEY";
         const map = new Map ({
           basemap: "arcgis-topographic" //Basemap layer service
          const webmap = new WebMap({
            portalItem: {
              id: "41281c51f9de45edaf1c8ed44bb10e30"
            }
          });
    
          const view = new MapView({
            container: "viewDiv",
    
            map: map,
            center: [-118.80500,34.02700],
            zoom: 13
    
            map: webmap
    
          });
          const scalebar = new ScaleBar({
            view: view
          view.ui.add(scalebar, "bottom-left");
        const legend = new Legend ({
            view: view
          view.ui.add(legend, "top-right");
      </script>
    </head>
    <body>
      <div id="viewDiv"></div>
    </body>
    </html>
  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 non-metric values.

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

    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
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    <html>
    <head>
      <meta charset="utf-8">
      <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no">
      <title>ArcGIS API for JavaScript Tutorials: Display a Web Map</title>
      <style>
        html, body, #viewDiv {
          padding: 0;
          margin: 0;
          height: 100%;
          width: 100%;
      </style>
      <link rel="stylesheet" href="https://js.arcgis.com/4.20/esri/themes/light/main.css">
      <script src="https://js.arcgis.com/4.20/"></script>
    
      <script>
        require([
          "esri/config",
          "esri/Map",
          "esri/WebMap",
          "esri/views/MapView",
          "esri/widgets/ScaleBar",
          "esri/widgets/Legend"
        ], function(esriConfig, WebMap, MapView, ScaleBar, Legend) {
        esriConfig.apiKey = "YOUR_API_KEY";
         const map = new Map ({
           basemap: "arcgis-topographic" //Basemap layer service
          const webmap = new WebMap({
            portalItem: {
              id: "41281c51f9de45edaf1c8ed44bb10e30"
          const view = new MapView({
            container: "viewDiv",
    
            map: map,
            center: [-118.80500,34.02700],
            zoom: 13
            map: webmap
    
          });
    
          const scalebar = new ScaleBar({
            view: view
          });
    
          view.ui.add(scalebar, "bottom-left");
    
        const legend = new Legend ({
            view: view
          view.ui.add(legend, "top-right");
      </script>
    </head>
    <body>
      <div id="viewDiv"></div>
    </body>
    </html>
  2. Create a Legend to display feature information. Add the legend to the top-right of the view.

    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
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    <html>
    <head>
      <meta charset="utf-8">
      <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no">
      <title>ArcGIS API for JavaScript Tutorials: Display a Web Map</title>
      <style>
        html, body, #viewDiv {
          padding: 0;
          margin: 0;
          height: 100%;
          width: 100%;
      </style>
      <link rel="stylesheet" href="https://js.arcgis.com/4.20/esri/themes/light/main.css">
      <script src="https://js.arcgis.com/4.20/"></script>
    
      <script>
        require([
          "esri/config",
          "esri/Map",
          "esri/WebMap",
          "esri/views/MapView",
          "esri/widgets/ScaleBar",
          "esri/widgets/Legend"
        ], function(esriConfig, WebMap, MapView, ScaleBar, Legend) {
        esriConfig.apiKey = "YOUR_API_KEY";
         const map = new Map ({
           basemap: "arcgis-topographic" //Basemap layer service
          const webmap = new WebMap({
            portalItem: {
              id: "41281c51f9de45edaf1c8ed44bb10e30"
          const view = new MapView({
            container: "viewDiv",
            map: map,
            center: [-118.80500,34.02700],
            zoom: 13
            map: webmap
          const scalebar = new ScaleBar({
            view: view
          });
    
          view.ui.add(scalebar, "bottom-left");
    
        const legend = new Legend ({
            view: view
          });
          view.ui.add(legend, "top-right");
      </script>
    </head>
    <body>
      <div id="viewDiv"></div>
    </body>
    </html>

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.