Learn how to add a styled vector basemap layer to a map .
A basemap layer provides the visual context for a map . You can use the default basemap layer styles provided by the basemap layer service or you can create your own custom styles with the ArcGIS Vector Tile Style Editor for specific mapping applications.
In this tutorial you use OpenLayers to display a styled vector basemap layer in a map .
PrerequisitesYou 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 penTo get started, either complete the Display a map tutorial or use this pen . Set the API keyTo access ArcGIS location services , you need an API key .
Go to your dashboard to get an API key .
In CodePen , update apiKey
to use your key.
Change line
const apiKey = "YOUR-API-KEY" ;
const basemapId = "ArcGIS:Streets" ;
const basemapURL = "https://basemaps-api.arcgis.com/arcgis/rest/services/styles/" + basemapId + "?type=style&apiKey=" + apiKey;
olms(map, basemapURL);
Add the vector tile layerYou can access a basemap layer by referencing its item ID . You can find a layer's item ID by accessing it inArcGIS 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.
Change the basemapId
from ArcGIS:Streets
to your own item ID.
Show more lines
Change line
<!DOCTYPE html>
< html >
< head >
< meta charset = "utf-8" />
< meta name = "viewport" content = "initial-scale=1, maximum-scale=1, user-scalable=no" />
< title > OpenLayers Tutorials: Add a styled basemap layer </ title >
< style >
html ,
body ,
#map {
padding : 0 ;
margin : 0 ;
height : 100% ;
width : 100% ;
font-family : Arial, Helvetica, sans-serif;
font-size : 14px ;
color : #323232 ;
}
</ style >
< link rel = "stylesheet" href = "https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.4.3/css/ol.css" type = "text/css" />
< script src = "https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.4.3/build/ol.js" > </ script >
< script src = "https://cdn.jsdelivr.net/npm/ol-mapbox-style@6.1.4/dist/olms.js" > </ script >
</ head >
< body >
< div id = "map" > </ div >
< script >
const map = new ol.Map({ target: "map" });
map.setView(
new ol.View({
center: ol.proj.fromLonLat([ -118.805 , 34.027 ]),
zoom: 8
})
);
const apiKey = "YOUR-API-KEY" ;
const basemapId = "6976148c11bd497d8624206f9ee03e30" ; // Custom vector tile style
const basemapURL = "https://basemaps-api.arcgis.com/arcgis/rest/services/styles/" + basemapId + "?type=style&apiKey=" + apiKey;
olms(map, basemapURL);
</ script >
</ body >
</ html >
Show more lines
If you would like to use your own vector tile layer or basemap layer style in your app, and if you have a developer account, you can use an API key to access them. Use the developer dashboard to configure your API key to support accessing Basemap layers and Content and items . If you have a layer, and you have an ArcGIS Online or Enterprise account, you cannot use API keys to access Private layers and items, and you need to use OAuth 2.0 to access them. Public layers, however, can be accessed by all accounts. Learn more in Security and authentication - OAuth2.0 .
Run the appIn CodePen , run your code to display the map.
It should display the Forest and Parks Canvas vector basemap layer.
What's next?Learn how to use additional ArcGIS location services in these tutorials: