Learn how to access and display a styled vector basemap layer in a map.
A styled basemap layer is a set of styles that you define to override one of the default basemap styles service vector tile layer styles. These are used to create and display a map or scene with your own custom styles, labels, and colors. To create a styled basemap layer, you can use the ArcGIS Vector Tile Style Editor. The editor stores your styles in ArcGIS as a layer item with an item ID.
In this tutorial, you will use an item ID to access and display a styled vector tile layer (Forest and Parks Canvas) in a map. You also add an image tile layer (World Hillshade) to enhance the visualization. Both layers are hosted 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, add theBasemap
,Vector
, andTile Layer Tile
modules.Layer 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 require([ "esri/config", "esri/Map", "esri/views/MapView", "esri/Basemap", "esri/layers/VectorTileLayer", "esri/layers/TileLayer", ], function (esriConfig, Map, MapView, Basemap, VectorTileLayer, TileLayer) { esriConfig.apiKey = "YOUR_ACCESS_TOKEN";
Create a vector tile layer
You can access a basemap layer by referencing its item ID. You can find a layer's item ID by accessing it with ArcGIS Online or the ArcGIS Vector Tile Style Editor.
-
Go to the Forest and Parks Canvas vector tile layer in ArcGIS Online and find its item ID. The ID is at the end of the URL.
-
In CodePen, create a new
Vector
object and set itsTile Layer portal
Item id
property tod2ff12395aeb45998c1b154e25d680e5
and theopacity
property to0.75
.Learn more about loading a layer by its item ID in the API reference.
Use dark colors for code blocks const vectorTileLayer = new VectorTileLayer({ portalItem: { id: "6976148c11bd497d8624206f9ee03e30" // Forest and Parks Canvas }, opacity: 0.75 });
Create an image tile layer
Use ArcGIS Online to find the item ID for the World Hillshade image tile layer and then use it to access the layer. The image tile layer will be used to visually enhance the styles with terrain.
-
Go to the World Hillshade image tile layer in ArcGIS and find its item ID. The ID is at the end of the URL.
-
In CodePen, create a new
Tile
and set itsLayer portal
Item id
property to1b243539f4514b6ba35e7d995890db1d
.Learn more about loading a layer by its item ID in the API reference.
Use dark colors for code blocks const vectorTileLayer = new VectorTileLayer({ portalItem: { id: "6976148c11bd497d8624206f9ee03e30" // Forest and Parks Canvas }, opacity: 0.75 }); const imageTileLayer = new TileLayer({ portalItem: { id: "1b243539f4514b6ba35e7d995890db1d" // World Hillshade } });
Add the basemap layers
Basemaps can contain multiple baselayers
. Use the Basemap
class to add the vector
and image
created above. These layers will be blended together to create the underlying style for the map. The vector tile layer provides the base colors and the image tile layer provides the hillshade or topographic effect.
-
In the main function, create a
Basemap
object and addvector
and theTile Layer image
to theTile Layer baselayers
array.Use dark colors for code blocks const vectorTileLayer = new VectorTileLayer({ portalItem: { id: "6976148c11bd497d8624206f9ee03e30" // Forest and Parks Canvas }, opacity: 0.75 }); const imageTileLayer = new TileLayer({ portalItem: { id: "1b243539f4514b6ba35e7d995890db1d" // World Hillshade } }); const basemap = new Basemap({ baseLayers: [ imageTileLayer, vectorTileLayer ] });
-
Update the
basemap
property to use thebasemap
object created earlier.Use dark colors for code blocks const basemap = new Basemap({ baseLayers: [ imageTileLayer, vectorTileLayer }); const map = new Map({ basemap: basemap, });
Update the map view
- Update the
center
andzoom
properties to zoom the display to North America.Use dark colors for code blocks const view = new MapView({ container: "viewDiv", map: map, center: [-100, 40], zoom: 3 });
Run the app
In CodePen, run your code to display the map.
The map view should display the Forest and Parks Canvas vector tile layer on top of the World Hillshade image tile layer. The styled vector tile layer is displayed on top of the hillshade terrain.
What's next?
Learn how to use additional API features and ArcGIS services in these tutorials: