Learn how to access and display a styled vector basemap layer in a map.
Pan and zoom the map to explore the custom vector tile styles.
A styled
In this tutorial, you will use an
Prerequisites
Steps
Create a new pen
- To get started, either complete the Display a map tutorial or
.
Get an access token
You need an
- Go to the Create an API key tutorial and create an
API key with the followingprivilege(s) :
- Privileges
- Location services > Basemaps
- Privileges
- In CodePen, set the
apiKeyproperty on the globalesriConfigvariable to your access token.var esriConfig = {apiKey: "YOUR_ACCESS_TOKEN",};
To learn about other ways to get an access token, go to Types of authentication.
Add modules
-
In a new
<script>at the bottom of the<body>, add theBasemap,VectorTileLayer, andTileLayermodules.The
ArcGIS Maps SDK for JavaScript is available via CDN and npm, but this tutorial is based on CDN. The$arcgis.importglobal function accepts a module path or array of module paths, and returns a promise that resolves with the requested modules. This function can only be used when working with the CDN; otherwise, use the standard import syntax. To learn more about the SDK’s different modules, visit the References page.33 collapsed lines<html><head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /><title>ArcGIS Maps SDK for JavaScript Tutorials: Display a custom basemap style</title><style>html,body {height: 100%;margin: 0;}</style><script>var esriConfig = { apiKey: "YOUR_ACCESS_TOKEN" };</script><!-- Load the ArcGIS Maps SDK for JavaScript from CDN --><script type="module" src="https://js.arcgis.com/5.0/"></script></head><body><arcgis-map basemap="arcgis/topographic" center="-118.805, 34.020" zoom="13"><arcgis-zoom slot="top-left"></arcgis-zoom></arcgis-map><script type="module">const [Basemap, TileLayer, VectorTileLayer] = await $arcgis.import(["@arcgis/core/Basemap.js","@arcgis/core/layers/TileLayer.js","@arcgis/core/layers/VectorTileLayer.js",]);</script>5 collapsed lines</body></html>
Create a vector tile layer
You can access a basemap layer by referencing its
-
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
VectorTileLayerobject and set itsportalItemidproperty tod2ff12395aeb45998c1b154e25d680e5and theopacityproperty to0.75.Learn more about loading a layer by its item ID in the API reference.
41 collapsed lines<html><head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /><title>ArcGIS Maps SDK for JavaScript Tutorials: Display a custom basemap style</title><style>html,body {height: 100%;margin: 0;}</style><script>var esriConfig = { apiKey: "YOUR_ACCESS_TOKEN" };</script><!-- Load the ArcGIS Maps SDK for JavaScript from CDN --><script type="module" src="https://js.arcgis.com/5.0/"></script></head><body><arcgis-map basemap="arcgis/topographic" center="-118.805, 34.020" zoom="13"><arcgis-zoom slot="top-left"></arcgis-zoom></arcgis-map><script type="module">const [Basemap, TileLayer, VectorTileLayer] = await $arcgis.import(["@arcgis/core/Basemap.js","@arcgis/core/layers/TileLayer.js","@arcgis/core/layers/VectorTileLayer.js",]);const vectorTileLayer = new VectorTileLayer({portalItem: {id: "6976148c11bd497d8624206f9ee03e30", // Forest and Parks Canvas},opacity: 0.75,});6 collapsed lines</script></body></html>
Create an image tile layer
Use
-
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
TileLayerand set itsportalItemidproperty to1b243539f4514b6ba35e7d995890db1d.Learn more about loading a layer by its item ID in the API reference.
41 collapsed lines
<html>
<head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /> <title>ArcGIS Maps SDK for JavaScript Tutorials: Display a custom basemap style</title>
<style> html, body { height: 100%; margin: 0; } </style>
<script> var esriConfig = { apiKey: "YOUR_ACCESS_TOKEN" }; </script>
<!-- Load the ArcGIS Maps SDK for JavaScript from CDN --> <script type="module" src="https://js.arcgis.com/5.0/"></script>
</head>
<body>
<arcgis-map basemap="arcgis/topographic" center="-118.805, 34.020" zoom="13">
<arcgis-zoom slot="top-left"></arcgis-zoom>
</arcgis-map>
<script type="module">
const [Basemap, TileLayer, VectorTileLayer] = await $arcgis.import([ "@arcgis/core/Basemap.js", "@arcgis/core/layers/TileLayer.js", "@arcgis/core/layers/VectorTileLayer.js", ]);
const vectorTileLayer = new VectorTileLayer({ portalItem: { id: "6976148c11bd497d8624206f9ee03e30", // Forest and Parks Canvas }, opacity: 0.75, });
const hillshadeTileLayer = new TileLayer({ portalItem: { id: "1b243539f4514b6ba35e7d995890db1d", // World Hillshade }, });
6 collapsed lines
</script>
</body>
</html>Add the basemap layers
Basemaps can contain multiple baseLayers. Use the Basemap class to add the vectorTileLayerand hillshadeTileLayer created above. These layers will create the underlying style for the map. The vector tile layer provides the base colors and the image tile layer provides the
-
In the main function, create a
Basemapobject and addvectorTileLayerand thehillshadeTileLayerto thebaseLayersarray.41 collapsed lines<html><head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /><title>ArcGIS Maps SDK for JavaScript Tutorials: Display a custom basemap style</title><style>html,body {height: 100%;margin: 0;}</style><script>var esriConfig = { apiKey: "YOUR_ACCESS_TOKEN" };</script><!-- Load the ArcGIS Maps SDK for JavaScript from CDN --><script type="module" src="https://js.arcgis.com/5.0/"></script></head><body><arcgis-map basemap="arcgis/topographic" center="-118.805, 34.020" zoom="13"><arcgis-zoom slot="top-left"></arcgis-zoom></arcgis-map><script type="module">const [Basemap, TileLayer, VectorTileLayer] = await $arcgis.import(["@arcgis/core/Basemap.js","@arcgis/core/layers/TileLayer.js","@arcgis/core/layers/VectorTileLayer.js",]);const vectorTileLayer = new VectorTileLayer({portalItem: {id: "6976148c11bd497d8624206f9ee03e30", // Forest and Parks Canvas},opacity: 0.75,});const hillshadeTileLayer = new TileLayer({portalItem: {id: "1b243539f4514b6ba35e7d995890db1d", // World Hillshade},});const basemap = new Basemap({ baseLayers: [hillshadeTileLayer, vectorTileLayer] });6 collapsed lines</script></body></html> -
Update the map’s
basemapproperty to use thebasemapobject created earlier.41 collapsed lines<html><head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /><title>ArcGIS Maps SDK for JavaScript Tutorials: Display a custom basemap style</title><style>html,body {height: 100%;margin: 0;}</style><script>var esriConfig = { apiKey: "YOUR_ACCESS_TOKEN" };</script><!-- Load the ArcGIS Maps SDK for JavaScript from CDN --><script type="module" src="https://js.arcgis.com/5.0/"></script></head><body><arcgis-map basemap="arcgis/topographic" center="-118.805, 34.020" zoom="13"><arcgis-zoom slot="top-left"></arcgis-zoom></arcgis-map><script type="module">const [Basemap, TileLayer, VectorTileLayer] = await $arcgis.import(["@arcgis/core/Basemap.js","@arcgis/core/layers/TileLayer.js","@arcgis/core/layers/VectorTileLayer.js",]);const vectorTileLayer = new VectorTileLayer({portalItem: {id: "6976148c11bd497d8624206f9ee03e30", // Forest and Parks Canvas},opacity: 0.75,});const hillshadeTileLayer = new TileLayer({portalItem: {id: "1b243539f4514b6ba35e7d995890db1d", // World Hillshade},});const basemap = new Basemap({ baseLayers: [hillshadeTileLayer, vectorTileLayer] });const viewElement = document.querySelector("arcgis-map");viewElement.basemap = basemap;6 collapsed lines</script></body></html>
Update the map position
-
Update the
centerandzoomproperties to zoom the display to North America.56 collapsed lines<html><head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /><title>ArcGIS Maps SDK for JavaScript Tutorials: Display a custom basemap style</title><style>html,body {height: 100%;margin: 0;}</style><script>var esriConfig = { apiKey: "YOUR_ACCESS_TOKEN" };</script><!-- Load the ArcGIS Maps SDK for JavaScript from CDN --><script type="module" src="https://js.arcgis.com/5.0/"></script></head><body><arcgis-map basemap="arcgis/topographic" center="-118.805, 34.020" zoom="13"><arcgis-zoom slot="top-left"></arcgis-zoom></arcgis-map><script type="module">const [Basemap, TileLayer, VectorTileLayer] = await $arcgis.import(["@arcgis/core/Basemap.js","@arcgis/core/layers/TileLayer.js","@arcgis/core/layers/VectorTileLayer.js",]);const vectorTileLayer = new VectorTileLayer({portalItem: {id: "6976148c11bd497d8624206f9ee03e30", // Forest and Parks Canvas},opacity: 0.75,});const hillshadeTileLayer = new TileLayer({portalItem: {id: "1b243539f4514b6ba35e7d995890db1d", // World Hillshade},});const basemap = new Basemap({ baseLayers: [hillshadeTileLayer, vectorTileLayer] });const viewElement = document.querySelector("arcgis-map");viewElement.basemap = basemap;viewElement.center = [-100, 40];viewElement.zoom = 3;7 collapsed lines</script></body></html>
Run the app
In CodePen, run your code to display the map.
The map 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 SDK features and ArcGIS services in these tutorials: