Learn how to add a map tile layer
A map tile layer displays raster imagery such as satellite photography or hillshading. You can combine map tile layers to enhance the display of a street basemap
In this tutorial, you add a Santa Monica contours map tile layer on top of a basemap.
Prerequisites
You need an ArcGIS Location Platform or ArcGIS Online account.
Steps
Get the starter app
Select a type of authentication and follow the steps to create a new app.
Choose API key authentication if you:
- Want the easiest way to get started.
- Want to build public applications
A public application is an application that allows anonymous access without requiring users to sign in with an ArcGIS account. It supports API key or app authentication. that access ArcGIS Location ServicesArcGIS Location Services, also referred to as Location Services, are services hosted by Esri that provide geospatial functionality for developing mapping applications. They include the ArcGIS Basemap Styles service, ArcGIS Static Basemap Tiles service, ArcGIS Places service, ArcGIS Geocoding service, ArcGIS Routing service, ArcGIS GeoEnrichment service, and ArcGIS Elevation service. An ArcGIS Location Platform or ArcGIS Online account is required to use the services. and secure itemsAn item, also known as a content item, is a resource stored in a portal such as a web map, hosted layer, style, script tool, file, or notebook. . - Have an ArcGIS Location Platform or ArcGIS Online account.
Choose user authentication if you:
- Want to build private applications.
- Require application users to sign in with their own ArcGIS account
An ArcGIS account is an identity with a user type and set of privileges that can access specific ArcGIS products, tools, APIs, services, and resources. The main account types that can be used for development are an ArcGIS Location Platform account, ArcGIS Online account, and ArcGIS Enterprise account. ArcGIS Location Platform and ArcGIS Online accounts are also associated with a subscription. and access resources their behalf. - Have an ArcGIS Online account.
To learn more about both types of authentication, go to Authentication.
Set up authentication
Set developer credentials
Use the API key or OAuth developer credentials
Update the map
-
Center the map on
[-118.44, 34.03]and set the zoom level to 11. This will set the map focus to Santa Monica, California.Use dark colors for code blocks Copy <script> /* Use for API key authentication */ const accessToken = "YOUR_ACCESS_TOKEN"; // or /* Use for user authentication */ // const session = await arcgisRest.ArcGISIdentityManager.beginOAuth2({ // clientId: "YOUR_CLIENT_ID", // Your client ID from OAuth credentials // redirectUri: "YOUR_REDIRECT_URI", // The redirect URL registered in your OAuth credentials // portal: "YOUR_PORTAL_URL" // Your portal URL // }) // const accessToken = session.token; const map = new maplibregl.Map({ container: "map", // the id of the div element zoom: 11, // starting zoom center: [-118.44, 34.03] // starting location [longitude, latitude] }); const basemapStyle = maplibreArcGIS.BasemapStyle.applyStyle(map, { style: "arcgis/light-gray", token: accessToken }); </script>
Add a load event handler
To add layers to the map, you need to use the load event to ensure the map is fully loaded.
-
Add an event handler for the
loadevent.For more information about the
loadevent, see the MapLibre GL JS documentation.Use dark colors for code blocks <script> /* Use for API key authentication */ const accessToken = "YOUR_ACCESS_TOKEN"; // or /* Use for user authentication */ // const session = await arcgisRest.ArcGISIdentityManager.beginOAuth2({ // clientId: "YOUR_CLIENT_ID", // Your client ID from OAuth credentials // redirectUri: "YOUR_REDIRECT_URI", // The redirect URL registered in your OAuth credentials // portal: "YOUR_PORTAL_URL" // Your portal URL // }) // const accessToken = session.token; const map = new maplibregl.Map({ container: "map", // the id of the div element zoom: 11, // starting zoom center: [-118.44, 34.03] // starting location [longitude, latitude] }); const basemapStyle = maplibreArcGIS.BasemapStyle.applyStyle(map, { style: "arcgis/light-gray", token: accessToken }); map.once("load", () => { // This code runs once the base style has finished loading. }); </script>
Add a raster source
You need to define a sourceMap how to access the data for the layer, but does not display it.
-
Inside the load event handler, add a raster source with id
contours.While there are several types of source, the most common are
vectorfor vector tiles,rasterfor map tiles, andgeojsonfor a set of features represented as GeoJSON.For more information, see the MapLibre Style Specification
Use dark colors for code blocks map.once("load", () => { // This code runs once the base style has finished loading. map.addSource("contours", { type: "raster", tiles: [ "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Santa_Monica_contours_(map_tiles)/MapServer/tile/{z}/{y}/{x}?token=" + accessToken ], }); }); -
Add the data attribution
Data attribution is the requirement to display the names of data source providers in all applications when accessing ArcGIS content, layers, or services. for the map tile source.- Go to the Santa Monica Mountains contours item
An item, also known as a content item, is a resource stored in a portal such as a web map, hosted layer, style, script tool, file, or notebook. . - Scroll down to the Acknowledgments section and copy its value.
- Paste the copied value to the
attributionproperty.Use dark colors for code blocks map.once("load", () => { // This code runs once the base style has finished loading. map.addSource("contours", { type: "raster", tiles: [ "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Santa_Monica_contours_(map_tiles)/MapServer/tile/{z}/{y}/{x}?token=" + accessToken ], // Attribution text retrieved from https://arcgis.com/home/item.html?id=5fd812155876416997c1a03e841e8821 attribution: "City of Santa Monica GIS Data" }); });
- Go to the Santa Monica Mountains contours item
Add a raster layer
To display the map tiles, add a layerraster.
A layer
For more information, see the MapLibre Style Specification.
-
Add a raster layer with id
contours-raster, connected to thecontourssource.Use dark colors for code blocks map.once("load", () => { // This code runs once the base style has finished loading. map.addSource("contours", { type: "raster", tiles: [ "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Santa_Monica_contours_(map_tiles)/MapServer/tile/{z}/{y}/{x}?token=" + accessToken ], // Attribution text retrieved from https://arcgis.com/home/item.html?id=5fd812155876416997c1a03e841e8821 attribution: "City of Santa Monica GIS Data" }); map.addLayer( { id: "contours-raster", type: "raster", source: "contours" }, ); }); -
At the top right, click Run. You should see the contours layer only.
Adjust the basemap
By default, the contours layer is added on top of all the basemap layers
-
In the load handler, find the id of the first non-background layer. Modify your
addcall to insert the raster layer before that layer.Layer Use dark colors for code blocks // Find the first non-background layer in order to add our raster layer before it. const layers = map.getStyle().layers; const bottomLayer = layers.find((layer) => layer.type !== "background"); map.addLayer( { id: "contours-raster", type: "raster", source: "contours" }, bottomLayer.id ); -
Set 50%
fill-opacityfor every fill layer except water and building layers.Use dark colors for code blocks bottomLayer.id ); layers.forEach((layer) => { if (layer.type === "fill" && !layer.id.match(/(Water area|Marine area|Bathymetry|Building)/)) { map.setPaintProperty(layer.id, "fill-opacity", 0.5); } });
Run the app
Run the app.
Your map should display a semi-transparent contours layer on top of a basemap.What's next?
Learn how to use additional location services in these tutorials:


