Change the basemap layer

Learn how to change the basemap layer in a map.

The basemap layer service provides a number of basemap layer styles such as topography, streets, and imagery that you can use in maps.

In this tutorial, you use the Basemap Toggle and BasemapGallery widgets to select and display different basemap layers.

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, add the Basemap Toggle and BasemapGallery 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.

    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: Change the basemap layer</title>
      <style>
        html, body, #viewDiv {
          padding: 0;
          margin: 0;
          height: 100%;
          width: 100%;
      </style>
    
      <link rel="stylesheet" href="https://js.arcgis.com/4.19/esri/themes/light/main.css">
      <script src="https://js.arcgis.com/4.19/"></script>
    
      <script>
        require([
          "esri/config",
          "esri/Map",
          "esri/views/MapView",
    
          "esri/widgets/BasemapToggle",
          "esri/widgets/BasemapGallery"
    
        ], function(esriConfig, Map, MapView, BasemapToggle, BasemapGallery) {
          esriConfig.apiKey = "YOUR_API_KEY";
          const map = new Map({
            basemap: "arcgis-topographic"
          const view = new MapView({
            container: "viewDiv",
            map: map,
            center: [-118.80543,34.02700],
            zoom: 13
          const basemapToggle = new BasemapToggle({
            view: view,
            nextBasemap: "arcgis-imagery"
          view.ui.add(basemapToggle,"bottom-right");
          const basemapGallery = new BasemapGallery({
            view: view,
            source: {
              query: {
                title: '"World Basemaps for Developers" AND owner:esri'
          view.ui.add(basemapGallery, "top-right"); // Add to the view
      </script>
    </head>
    <body>
      <div id="viewDiv"></div>
    </body>
    </html>

Toggle between basemaps

An easy way to enable selection between two basemaps is to use the Basemap Toggle widget. Use the widget to toggle between arcgis-topographic and arcgis-imagery basemaps.

  1. Create a Basemap Toggle and set the view. Set the nextBasemap property to arcgis-imagery.

    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: Change the basemap layer</title>
      <style>
        html, body, #viewDiv {
          padding: 0;
          margin: 0;
          height: 100%;
          width: 100%;
      </style>
    
      <link rel="stylesheet" href="https://js.arcgis.com/4.19/esri/themes/light/main.css">
      <script src="https://js.arcgis.com/4.19/"></script>
    
      <script>
        require([
          "esri/config",
          "esri/Map",
          "esri/views/MapView",
          "esri/widgets/BasemapToggle",
          "esri/widgets/BasemapGallery"
        ], function(esriConfig, Map, MapView, BasemapToggle, BasemapGallery) {
          esriConfig.apiKey = "YOUR_API_KEY";
          const map = new Map({
            basemap: "arcgis-topographic"
          const view = new MapView({
            container: "viewDiv",
            map: map,
            center: [-118.80543,34.02700],
            zoom: 13
          });
    
          const basemapToggle = new BasemapToggle({
            view: view,
            nextBasemap: "arcgis-imagery"
         });
    
          view.ui.add(basemapToggle,"bottom-right");
          const basemapGallery = new BasemapGallery({
            view: view,
            source: {
              query: {
                title: '"World Basemaps for Developers" AND owner:esri'
          view.ui.add(basemapGallery, "top-right"); // Add to the view
      </script>
    </head>
    <body>
      <div id="viewDiv"></div>
    </body>
    </html>
  2. Add the widget to the bottom-right corner of of the view.

    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: Change the basemap layer</title>
      <style>
        html, body, #viewDiv {
          padding: 0;
          margin: 0;
          height: 100%;
          width: 100%;
      </style>
    
      <link rel="stylesheet" href="https://js.arcgis.com/4.19/esri/themes/light/main.css">
      <script src="https://js.arcgis.com/4.19/"></script>
    
      <script>
        require([
          "esri/config",
          "esri/Map",
          "esri/views/MapView",
          "esri/widgets/BasemapToggle",
          "esri/widgets/BasemapGallery"
        ], function(esriConfig, Map, MapView, BasemapToggle, BasemapGallery) {
          esriConfig.apiKey = "YOUR_API_KEY";
          const map = new Map({
            basemap: "arcgis-topographic"
          const view = new MapView({
            container: "viewDiv",
            map: map,
            center: [-118.80543,34.02700],
            zoom: 13
          });
    
          const basemapToggle = new BasemapToggle({
            view: view,
            nextBasemap: "arcgis-imagery"
         });
    
          view.ui.add(basemapToggle,"bottom-right");
          const basemapGallery = new BasemapGallery({
            view: view,
            source: {
              query: {
                title: '"World Basemaps for Developers" AND owner:esri'
          view.ui.add(basemapGallery, "top-right"); // Add to the view
      </script>
    </head>
    <body>
      <div id="viewDiv"></div>
    </body>
    </html>
  3. Switch between basemaps.

You can also use the BasemapGallery widget to select different basemaps.

  1. Create a BasemapGallery and set the query in the source property to search for the "World Basemaps for Developers" basemap group.

    In a future release, you will not need to set the source when using API keys.

    Add line.Add line.Add line.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: Change the basemap layer</title>
      <style>
        html, body, #viewDiv {
          padding: 0;
          margin: 0;
          height: 100%;
          width: 100%;
      </style>
    
      <link rel="stylesheet" href="https://js.arcgis.com/4.19/esri/themes/light/main.css">
      <script src="https://js.arcgis.com/4.19/"></script>
    
      <script>
        require([
          "esri/config",
          "esri/Map",
          "esri/views/MapView",
          "esri/widgets/BasemapToggle",
          "esri/widgets/BasemapGallery"
        ], function(esriConfig, Map, MapView, BasemapToggle, BasemapGallery) {
          esriConfig.apiKey = "YOUR_API_KEY";
          const map = new Map({
            basemap: "arcgis-topographic"
          const view = new MapView({
            container: "viewDiv",
            map: map,
            center: [-118.80543,34.02700],
            zoom: 13
          const basemapToggle = new BasemapToggle({
            view: view,
            nextBasemap: "arcgis-imagery"
          view.ui.add(basemapToggle,"bottom-right");
    
          const basemapGallery = new BasemapGallery({
            view: view,
            source: {
              query: {
                title: '"World Basemaps for Developers" AND owner:esri'
              }
            }
          });
    
          view.ui.add(basemapGallery, "top-right"); // Add to the view
      </script>
    </head>
    <body>
      <div id="viewDiv"></div>
    </body>
    </html>
  2. Add the widget to the top-right corner of the view.

    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: Change the basemap layer</title>
      <style>
        html, body, #viewDiv {
          padding: 0;
          margin: 0;
          height: 100%;
          width: 100%;
      </style>
    
      <link rel="stylesheet" href="https://js.arcgis.com/4.19/esri/themes/light/main.css">
      <script src="https://js.arcgis.com/4.19/"></script>
    
      <script>
        require([
          "esri/config",
          "esri/Map",
          "esri/views/MapView",
          "esri/widgets/BasemapToggle",
          "esri/widgets/BasemapGallery"
        ], function(esriConfig, Map, MapView, BasemapToggle, BasemapGallery) {
          esriConfig.apiKey = "YOUR_API_KEY";
          const map = new Map({
            basemap: "arcgis-topographic"
          const view = new MapView({
            container: "viewDiv",
            map: map,
            center: [-118.80543,34.02700],
            zoom: 13
          const basemapToggle = new BasemapToggle({
            view: view,
            nextBasemap: "arcgis-imagery"
          view.ui.add(basemapToggle,"bottom-right");
    
          const basemapGallery = new BasemapGallery({
            view: view,
            source: {
              query: {
                title: '"World Basemaps for Developers" AND owner:esri'
              }
            }
          });
    
          view.ui.add(basemapGallery, "top-right"); // Add to the view
      </script>
    </head>
    <body>
      <div id="viewDiv"></div>
    </body>
    </html>

Run the App

In CodePen, run your code to display the map.

You should have two widgets in your application that provide different ways to choose basemaps.

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.