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
- 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.
- Go to the Create an API key tutorial and create an API key with the following privilege(s):
- Privileges
- Location services > Basemaps
- Privileges
- In CodePen, set
esri
to your API key..Config.api Key Use dark colors for code blocks 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
-
In the
require
statement, delete theMap
module. Add theWeb
,Map Scale
, andBar Legend
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/
for loading the Map module. After the modules are loaded, they are passed as parameters (e.g.Map" 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. To learn more about the API's different modules visit the Overview Guide page.Use dark colors for code blocks <script> require([ "esri/config", "esri/Map", "esri/WebMap", "esri/views/MapView", "esri/widgets/ScaleBar", "esri/widgets/Legend" ], function(esriConfig, WebMap, MapView, ScaleBar, Legend) {
Load the web map
You can use the portal item ID to create a Web
. The web map will be passed to the view.
-
Delete the code that creates the
Map
.Use dark colors for code blocks esriConfig.apiKey = "YOUR_ACCESS_TOKEN"; const map = new Map ({ basemap: "arcgis/topographic" // basemap styles service });
-
Create a
Web
. Set theMap portal
ID toItem 41281c51f9de45edaf1c8ed44bb10e30
.Use dark colors for code blocks esriConfig.apiKey = "YOUR_ACCESS_TOKEN"; const webmap = new WebMap({ portalItem: { id: "41281c51f9de45edaf1c8ed44bb10e30" } });
-
Update the
map
property in theMap
with theView webmap
element. Remove thecenter
andzoom
properties, since the web map provides positioning information.Use dark colors for code blocks const webmap = new WebMap({ portalItem: { id: "41281c51f9de45edaf1c8ed44bb10e30" } }); const view = new MapView({ container: "viewDiv", map: map, center: [-118.80500,34.02700], zoom: 13, map: webmap });
-
At the top-right, click Run to verify that the web map has been successfully loaded and displays.
Add widgets
Use the Legend
and Scale
widgets to add more context to the application. The Legend
widget displays labels and symbols for layers visible in the view. The Scale
can display units in either metric or imperial values.
-
Create a
Scale
. Add theBar scalebar
to thebottom-left
of theview
.Use dark colors for code blocks const view = new MapView({ container: "viewDiv", map: webmap }); const scalebar = new ScaleBar({ view: view }); view.ui.add(scalebar, "bottom-left");
-
Create a
Legend
to display feature information. Add thelegend
to thetop-right
of theview
.Use dark colors for code blocks const scalebar = new ScaleBar({ view: view }); view.ui.add(scalebar, "bottom-left"); const legend = new Legend ({ view: view }); view.ui.add(legend, "top-right");
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: