Learn how to create and display a map with a basemap layer.
A map contains layers of geographic data. A map contains a basemap layer and, optionally, one or more data layers. You can display a specific area of a map using a map view and setting the location and zoom level.
This tutorial shows you how to create and display a map of the Santa Monica Mountains in California using the topographic basemap layer.
The map and code in this tutorial will be used as the starting point for other 2D tutorials.
Prerequisites
Steps
Create a new pen
- Go to CodePen to create a new pen for your mapping application.
Add HTML
Define an HTML page to create a map that is the full width and height of the web browser window.
-
In CodePen > HTML, add HTML and CSS to create a page with a
view
element. TheDiv view
is the element displays the map and its CSS resets any browser settings so it can consume the full width and height of the browser.Div The
<!
tag is not required in CodePen. If you are using a different editor or running the page on a local server, be sure to add this tag to the top of your HTML page.DOCTYP E html > Use dark colors for code blocks <html> <head> <meta charset="utf-8" /> <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no" /> <title>ArcGIS Maps SDK for JavaScript Tutorials: Display a map</title> <style> html, body, #viewDiv { padding: 0; margin: 0; height: 100%; width: 100%; } </style> </head> <body> <div id="viewDiv"></div> </body> </html>
Reference the API
- In the
<head
tag, add references to the CSS file and JS library.> Use dark colors for code blocks <style> html, body, #viewDiv { padding: 0; margin: 0; height: 100%; width: 100%; } </style> <link rel="stylesheet" href="https://js.arcgis.com/4.30/esri/themes/light/main.css"> <script src="https://js.arcgis.com/4.30/"></script>
Add 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
) 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 Overview guide topic.
- In the
<head
tag, add a> <script
tag and a> require
statement to load theMap
andMap
modules. You can also add the JavaScript code to the CodePen > JS panel instead of the HTML panel. If you do so, remove theView <script
tag.> Use dark colors for code blocks <link rel="stylesheet" href="https://js.arcgis.com/4.30/esri/themes/light/main.css"> <script src="https://js.arcgis.com/4.30/"></script> <script> require(["esri/config", "esri/Map", "esri/views/MapView"], function(esriConfig, Map, MapView) { }); </script>
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:
- Privileges
- Location services > Basemaps
- Privileges
- Copy the API key as it will be used in the next step.
To learn about other ways to get an access token, go to Types of authentication.
Create a map
Use a Map
to set the basemap layer and apply your access token.
-
Go back to CodePen.
-
In the
require
statement, create a newMap
and set thebasemap
property toarcgis/topographic
. To enable access to the basemap styles service, set theapi
property of theKey Map
.A map can contain data layers and a basemap layer. In order to access a basemap layer from the basemap styles service, you need an access token. You can set the token from the previous step when you create the
Map
.Learn more about how a map and a map view work in Maps (2D) in the Mapping and location services guide.
Use dark colors for code blocks <script> require(["esri/config", "esri/Map", "esri/views/MapView"], function(esriConfig, Map, MapView) { esriConfig.apiKey = "YOUR_ACCESS_TOKEN"; const map = new Map({ basemap: "arcgis/topographic" // basemap styles service });
Create a map view
Use a Map
class to set the location of the map to display.
-
Create a
Map
and set theView map
property. To center the map view, set thecenter
property to-118.805, 34.027
and thezoom
property to13
. Set thecontainer
property toview
to display the contents of the map.Div The
Map
displays the contents of a map. TheView center
andzoom
properties determine the location of the map and the zoom level at which it is displayed when it loads.The
zoom
property sets the zoom level of the map. The values typically range from 0-20: 0 is the farthest from the earth's surface, and 20 is the closest. Some basemap layers can support additional zoom levels up to 23.The
Map
also supports a number of touch events such asView click
anddouble-click
. You can use these events to change the position of the map or to find features in layers.Learn more about how a map and a map view work in Maps (2D) in the Mapping and location services guide.
Use dark colors for code blocks esriConfig.apiKey = "YOUR_ACCESS_TOKEN"; const map = new Map({ basemap: "arcgis/topographic" // basemap styles service }); const view = new MapView({ map: map, center: [-118.805, 34.027], // Longitude, latitude zoom: 13, // Zoom level container: "viewDiv" // Div element });
Run the app
In CodePen, run your code to display the map.
The map should display the topographic basemap layer for an area of the Santa Monica Mountains in California.
What's next?
Learn how to use additional API features and ArcGIS services in these tutorials: