Display a map
Learn how to display a map with a basemap layer using OpenLayers.
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 by using a map view and setting the location and zoom level.
In this tutorial you will use OpenLayers to display a map of the Santa Monica Mountains in California using the topographic basemap layer.
This tutorial is the starting point for the other OpenLayers tutorials.
Prerequisites
You need an ArcGIS Developer or ArcGIS Online account to access the developer dashboard and create an API key.
Steps
Create a new pen
- Go to CodePen to create a new pen for your application.
Add HTML
Define an HTML page to create a map that is the full width and height of the browser window.
In CodePen > HTML, add HTML and CSS to create a page with a div element called
map
.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. Add line. Add line. Add line. Add line.
Load the libraries
You will need add <script>
and <link>
tags to load the JavaScript and CSS files for OpenLayers and ol-mapbox-style
.
In the
<head>
element, add references to the OpenLayers CSS and JavaScript library.Use dark colors for code blocks Add line. Add line. Add a reference to the
ol-mapbox-style
library, which is required to load and style a vector tile layer using a vector tile style in OpenLayers.Use dark colors for code blocks Add line.
Create a map and view
Use the Map
and View
classes to create an OpenLayers map.
Add a
<script>
element in the<body>
element.Use dark colors for code blocks Add line. Add line. Add line. Use the
Map
class to create the map with options to control its display and behavior. Set thetarget
property to"map"
, which is the id of the div element.Use dark colors for code blocks Add line. Create a
View
, and useset
to apply it to the map. To center the map view, set theView center
property to-118.80500,34.02700
and thezoom
property to13
.Use dark colors for code blocks Add line. Add line. Add line. Add line. Add line. Add line.
Get an API key
To access location services, you need an API key or OAuth 2.0 access token. To learn how to create and scope your key, visit the Create an API key tutorial.
- Go to your developer dashboard to get an API key.
- Copy the key as it will be used in the next step.
Add the basemap layer
OpenLayers does not directly support a vector basemap or a vector style file, so you will use the openlayers-mapbox-style
(olms
) JavaScript library to load a Mapbox style from the Basemap styles service and render it with OpenLayers.
Store your API key as a variable. You will need to include this API key whenever you invoke ArcGIS services.
Use dark colors for code blocks Add line. Create a
basemap
variable and set it toI d ArcGIS:
.Streets Use dark colors for code blocks Add line. Construct the basemap URL based on
basemap
and your API key.I d Use dark colors for code blocks Add line. Use
olms
to apply the basemap style to your map. Theolms
function takes two inputs, themap
element and the URL for a Mapbox basemap style file.Use dark colors for code blocks 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. Pan and zoom to explore the map.
What's next?
Learn how to use additional ArcGIS location services in these tutorials:

Change the basemap style
Switch a basemap style in a map.

Change the place label language
Switch the language of place labels on a basemap.

Display a custom vector tile style
Add a styled vector basemap layer to a map.

Search for an address
Find an address or place using a search box and the geocoding service.

Find a route and directions
Find a route and directions with the route service.