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 will use the Basemap Toggle and BasemapGallery widgets to select and display different basemap layers.
Prerequisites
You need an ArcGIS Developer or ArcGIS Online account to access the dashboard and create an API key.
Steps
Create a new pen
- To get started, either complete the Display a map tutorial or .
Set the API key
To access ArcGIS services, you need an API key.
Go to your dashboard to get an API key.
In CodePen, set the
api
to your key, so it can be used to access basemap layer and location services.Key Use dark colors for code blocks Change line
Add modules
In the
require
statement, add the Basemap Toggle and BasemapGallery modules.The ArcGIS Maps SDK for JavaScript is available as AMD modules and ES modules, but this tutorial is based on AMD. The AMD
require
function uses references to determine which modules will be loaded – for example, you can specify"esri/Map"
for loading the Map module. After the modules are loaded, they are passed as parameters (e.g.Map
) to the callback function where they can be used in your application. It is important to keep the module references and callback parameters in the same order. For more information on the different types of modules, visit the Introduction to Tooling guide topic.Use dark colors for code blocks Add line. Add line. Change line
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.
Create a Basemap Toggle and set the
view
. Set thenext
property toBasemap arcgis-imagery
.Use dark colors for code blocks Add line. Add line. Add line. Add line. Add the widget to the
bottom-right
corner of of theview
.Use dark colors for code blocks Add line. Switch between basemaps.
Select basemaps from a gallery
You can also use the BasemapGallery widget to select different basemaps.
Create a BasemapGallery and set the
query
in thesource
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.Use dark colors for code blocks Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add the widget to the
top-right
corner of theview
.Use dark colors for code blocks Add line.
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: