What is basemap usage?
As a developer, you can implement a basemap usage model in your application to control the type of basemap usage that occurs. You can use a model that results in usage for the number of basemap tiles consumed or the number of basemap sessions created.
Types of usage models
The two types of usage models you can implement with the ArcGIS Basemap Styles service are the following:
- Tile usage model: Usage for the total number of basemap tiles consumed.
- Session usage model: Usage for the total number of basemap sessions created.
Need help choosing a usage model?
The type of basemap usage model you choose depends on many factors such as the number of users that will use the application, the type of mapping interactivity it supports, and most importantly, the total number of tiles that it will consume.
The development requirements for each application are typically unique, but here are some key factors to consider:
- The phase of development you are in (prototyping, development, testing, or deployment).
- The total number of users that will use the application.
- The total number of basemap tiles an application will consume.
- The mapping functionality and user interaction requirements for the application.
- The number of basemap styles the application will display.
- The length of time and number of times users will access the application in a 12 hour period.
Learn more in the sections below.
Tile usage model
The tile usage model is an application programming pattern that results in the consumption of basemap tiles. Usage occurs for every request to the service that returns basemap tiles.
This usage model requires an access token to make requests directly to the basemap service for a style. The style request results in tiles returned to the application. Tiles are returned when a map loads and displays a basemap style, when the basemap style is changed, and when user interactions occur such as panning, zooming, or navigating the map.
How to use
To create an application that uses the tile usage model, you simply need an access token with the privilege to access basemaps. In most cases, you use API key authentication to get an access token. The access token can be used to access all styles available in the Basemap Styles service.
1. Get an access token
Go to the Create an API key tutorial to get an access token with the premium
privilege.
const accessToken = <YOUR_ACCESS_TOKEN>
2. Access a basemap style
Use the access token to create a style URL reference to the basemap service.
function loadMap(accessToken) {
const style = `https://basemapstyles-api.arcgis.com/arcgis/rest/services/styles/v2/styles/arcgis/outdoor?token=${accessToken}`;
const map = new maplibregl.Map({
container: 'map',
style: style,
zoom: 2,
center: [-30,20]
});
}
loadMap(accessToken);
Code example
Application that creates basemap tile charges
This example implements the basemap tile usage model. It uses an access token to access a style (and tiles) from the Basemap Styles service. To consume tiles effectively, the application starts at a focused geographic area. It also limits user interactions such as zooming to unsuitable levels, panning to unusable areas, and tilting the map to an unreasonable pitch.
This application creates basemap tile usage for all tiles consumed for all map interactions.
const accessToken = "YOUR_ACCESS_TOKEN"
// Access the basemap service and style
const map = new maplibregl.Map({
container: "map",
style: `https://basemapstyles-api.arcgis.com/arcgis/rest/services/styles/v2/styles/open/osm-style-relief?token=${accessToken}`,
zoom: 13, // starting zoom
center: [-119.0265, 37.639], // starting location [longitude, latitude]
maxZoom: 16, // limit zoom out
minZoom: 12, // limit zoom in
maxBounds: [ // limit map extent for pan
[-119.1486, 37.5667],
[-118.9147, 37.6956],
]
})
// Limit user interactions
map.dragRotate.disable()
map.touchZoomRotate.disableRotation()
map.setMaxPitch(0)
map.setMinPitch(0)
map.touchPitch.disable()
Session usage model
The session usage model is an application programming pattern that results in the creation of basemap sessions. A basemap session is a time frame during which a single user of a single application can use a session token to access unlimited basemap tiles from the Basemap Styles service.
This usage model requires creating and managing a basemap session in your application. When you create a basemap session, you can define the length of time the basemap session (and session token) is valid for. The maximum length of time for a session is 12 hours. When you create a session, you also need to define the basemap style family it applies to, either arcgis
or open
styles.
After a basemap session is created, you use the session token to access styles from the basemap service. A session token is used in a similar way to how access tokens are used to access basemap styles. However, a session token can only access the basemap service it was created from, and it can only access styles that belong to the basemap style family specified when it was created.
During the lifespan of a basemap session, you use the session token to make all requests to the basemap service for styles and tiles. This includes the initial request to load the map and style, requests to change the style in the map, and for all other requests that result from interactions such as panning, zooming, and map exploration. When the basemap session expires, you can create a new basemap session (and session token) to ensure uninterrupted usage.
Learn how to create and manage a basemap session in How to use.
How to use
To create an application that uses basemap sessions, you need an ArcGIS Location Platform account. You use the account to implement API Key authentication to get an access token. The access token must contain the basemap privilege (premium:user:basemaps) to access the service. You can use the access token to create a basemap session and get a session token. Once obtained, you use the session token to make all subsequent requests to the service for basemap styles and tiles. The session token cannot be used to access other services. When the session token expires, you can generate a new token by starting a new basemap session.
1. Get an access token
Go to the Create an API key tutorial to get an access token with the premium
privilege.
const accessToken = <YOUR_ACCESS_TOKEN>
2. Create a basemap session
Create a basemap session by making a request to the Basemap Styles service /sessions/start
endpoint with the access token. Include the style
parameter to define the type of basemap styles it will be able to access.
const createBasemapSession = async (accessToken) => {
const basemapSession = await fetch(`https://basemapstyles-api.arcgis.com/arcgis/rest/services/styles/v2/sessions/start?` + new URLSearchParams({
styleFamily:"arcgis", // Basemap styles the session token can access
durationSeconds: 43200, // Maximum duration (12hrs)
}).toString(),{
method:'GET',
headers: {
"Authentication":`Bearer ${accessToken}`
}
})
return await basemapSession.json();
}
let basemapSession = await createBasemapSession(accessToken);
3. Access a basemap style
Use the session token to access a basemap style in the style
and display a map.
const loadMap = (sessionToken, styleName) => {
const styleURL = `https://basemapstyles-api.arcgis.com/arcgis/rest/services/styles/v2/styles/${styleName}?token=${sessionToken}`;
if (!map) {
map = new maplibregl.Map({
container: 'map',
style: styleURL,
zoom: 2,
center: [-30,20]
});
} else {
map.setStyle(styleURL)
}
}
let basemapSession = await createBasemapSession(accessToken);
let map = loadMap(basemapSession.sessionToken, "arcgis/navigation");
4. Refresh when expired
Start a new session and update the map when the original session token is expired.
const isExpired = () {
return Date.now() > Date(basemapSession.endTime);
}
if (isExpired()) {
// Start new session
basemapSession = await createBasemapSession(accessToken);
// Update map with new style and token
map = loadMap(basemapSession.sessionToken, "arcgis/navigation");
}
Code example
Application that creates basemap session charges
This example implements the basemap session usage model. When the application starts, it uses an access token to create a basemap session for the Basemap Styles service. The session token is then used to access the basemap style and all basemap tiles. It is also used to access different styles if selected by the user. A timer is used to display when the session will expire and a new session is created if the user chooses to do so. It also creates a new basemap session if the application is restarted or refreshed.
While the basemap session is valid, this application supports unrestricted user interactions, such as panning, zooming, and changing basemap styles, and it allows users to navigate to any location around the world.
This application creates usage for a basemap session each time the application is run or restarted.
const initApp = async () => {
// Create the basemap session
basemapSession = await arcgisRest.BasemapStyleSession.start({
authentication: authentication,
styleFamily: "arcgis", // Access all styles in this family
duration: 43200 // 12 hours
})
// Session is expired, start a new session
basemapSession.on("expired", () => {
// Notify session is expired
alert("Session Expired. Click OK to continue.")
// Start a new session
basemapSession.refreshCredentials().then(() => {
displayMap(basemapSession.token)
})
})
// Display a basemap style with the session token
displayMap(basemapSession.token)
}