Overview
You will learn: how to build an app to load and display a web map from ArcGIS Online.
Applications can load and display 2D
In this tutorial, you will build an app that loads and displays a pre-configured web map stored in
Explore the web map layer styles and pop-ups in the map below.
Steps
Create a starter app
Open the JavaScript Starter App on CodePen.
In CodePen, click Fork and save the pen as
ArcGIS JavaScript Tutorials: Display a web map
.
Preview the web map
Go to the LA Trails and Parks Map to view the map in the Map Viewer. Make note of the ID at the end of the URL.
Display the web map in an app
In the
require
statement, remove theMap
module and add a reference to theWebMap
module.require([ "esri/WebMap", "esri/views/MapView" ], function(WebMap, MapView) {
At the beginning of the code in the main
function
, remove the code to create aMap
andMapView
and replace it with code to create a newWebMap
and a newMapView
. Set theportalItem
ID to41281c51f9de45edaf1c8ed44bb10e30
. Update the code in theMapView
to set thewebmap
to themap
property and comment the code to center and zoom the map. The web map provides the positioning information.//*** ADD ***// var webmap = new WebMap({ portalItem: { id: "41281c51f9de45edaf1c8ed44bb10e30" } }); var view = new MapView({ container: "viewDiv", //*** UPDATE ***// map: webmap //center: [-118.80500,34.02700], //zoom: 13 });
Run your code to view the web map. Be sure to click on the layers and zoom around!
Congratulations, you're done!
Your map app should look something like this.
Challenge
Add a legend and scalebar
To make your mapping app more meaningful, try adding a Legend
and ScaleBar
widget. Create a Legend
widget and then add it to the top-right
position of the view DefaultUI
. Now try adding a ScaleBar
to the bottom-left
corner. Visit the documentation to learn more about widgets and how to position them in the view.
require([
"esri/WebMap",
"esri/views/MapView",
//*** ADD ***//
"esri/widgets/Legend",
"esri/widgets/ScaleBar"
], function(WebMap, MapView, Legend, ScaleBar) {
...
//*** ADD ***//
var legend = new Legend({
view: view
});
view.ui.add(legend, "top-right");
var scalebar = new ScaleBar({
view: view
});
view.ui.add(scalebar, "bottom-left");
Explore other web maps
ArcGIS Online contains thousands of public web maps that can be loaded by applications. Go to