Overview
You will learn: how to build an app to interactively select and display an ArcGIS Online basemap.
Applications can access and display raster and vector tile basemaps hosted onBasemapToggle
or the BasemapGallery
widget. The BasemapToggle
widget allows you to switch between two basemaps that you define while the BasemapGallery
widget allows you to select from a number of basemaps that belong to an ArcGIS Online group. You can use a group of basemaps hosted by Esri or a group that you have composed specifically for your application. To learn more about designing and styling your own basemaps, visit the Style a vector basemap tutorial.
In this tutorial, you will use the BasemapToggle
and the BasemapGallery
widget to select and display basemaps hosted in ArcGIS Online.
Click the widget below to change basemaps.
Steps
Create a starter app
Open the JavaScript Starter App on CodePen.
In CodePen, click Fork and save the pen as
ArcGIS JavaScript Tutorials: Select a basemap
.
Toggle a basemap
One of the easiest ways to enable selection between two basemaps is to use the BasemapToggle
widget. In this step you will configure the widget to toggle between the Topographic and Satellite basemaps.
In the
require
statement, add a reference to theBasemapToggle
andBasemapGallery
modules.require([ "esri/Map", "esri/views/MapView", "esri/widgets/BasemapToggle", "esri/widgets/BasemapGallery" ], function(Map, MapView, BasemapToggle, BasemapGallery) {
At the end of the code in the main
function
, create a BasemapToggle widget. Set theview
and thenextBasemap
property tosatellite
.var basemapToggle = new BasemapToggle({ view: view, nextBasemap: "satellite" });
Then add the widget to the bottom right corner of the view by adding it to the view's
DefaultUI
.view.ui.add(basemapToggle, "bottom-right");
Run the app and try switching between basemaps.
Select a basemap from an ArcGIS Online group
Another way to select a basemap is to use the BasemapGallery
widget. In this step you will use the widget to load basemaps from an ArcGIS Online group so users can select and display them.
In the main
function
, comment out theBasemapToggle
widget code.In the main
function
, create aBasemapGallery
widget and configure it to load basemaps fromArcGIS Online . Set theview
to the active view and set thesource
to the ArcGIS Online portal. Set theurl
tohttps://wwww.arcgis.com
and setuseVectorBasemaps
totrue
so it loads the basemaps from the vector tile group. If you set this value tofalse
it will load the raster tile basemap group (see challenge step).var basemapGallery = new BasemapGallery({ view: view, source: { portal: { url: "https://www.arcgis.com", useVectorBasemaps: true // Load vector tile basemaps } } });
Add the widget to the top right corner of the view by adding it to the view's
DefaultUI
.view.ui.add(basemapGallery, "top-right");
Run the app and try switching between the different basemaps.
Congratulations, you're done!
Your app should look something like this.
Challenge
Load raster tile basemaps
Try changing the useVectorBasemaps
property to false
so that the raster tile basemaps are loaded into the BasemapGallery widget.
var basemapGallery = new BasemapGallery({
view: view,
source: {
portal: {
url: "https://www.arcgis.com",
//*** ADD ***//
useVectorBasemaps: false // Load raster tile basemaps
}
}
});