Display a map
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
You need a free ArcGIS developer account to access your dashboard and API keys. The API key must be scoped to access the services used in this tutorial.
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 Use dark colors for code blocks Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line.
Reference the API
In the
<head>
tag, add references to the CSS file and JS library.Use dark colors for code blocks Add line. Add line.
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/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.
In the
<head>
tag, add a<script>
tag and arequire
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 Add line. Add line. Add line. Add line. Add line.
Get an API key
An API key is required to access ArcGIS services.
- Go to your developer dashboard to get an API key.
- Copy the key as it will be used in the next step.
Create a map
Use a Map
to set the basemap layer and apply your API key.
Go back to CodePen.
In the
require
statement, create a newMap
and set thebasemap
property toarcgis-topographic
. To enable access to the Basemap layer service, set theapi
() property of theKey Map
.Use dark colors for code blocks Add line. Add line. Add line. Add line. Add line.
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.80500,34.02700
and thezoom
property to13
. Set thecontainer
property toview
to display the contents of the map.Div Use dark colors for code blocks Add line. Add line. Add line. Add line. Add line. Add line.
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: