Learn how to style vector tiles.
You can use MapLibre GL JS to display vector tile data using custom styles. The MapLibre style specification allows you to customize the fill, outline, opacity and other properties of vector tiles to display data effectively. If your vector tiles are published from a feature service, you can also perform data-driven visualizations based on attributes of the original feature service.
In this tutorial, you style land parcels from a public vector tile service according to their use type.
Prerequisites
An ArcGIS Location Platform or ArcGIS Online account.
Steps
Review the source data
This tutorial uses the Santa Monica Mountains Parcels vector tile service. This vector tile service was created by publishing a feature service as vector tiles using the portal. It contains the attributes of the original feature service, which can be accessed to style layers in your application. Find the original feature service in ArcGIS.com to view the names and values of different attributes.
-
Go to the item page for the Santa Monica Mountains Parcels vector tile layer.
-
Under Details, find the Created from property. Follow the link to view the item page for the original feature layer, Santa_Monica_Mountains_Parcels.
-
Click the Data tab to view the layer's features and attributes. Each feature represents a land parcel and has attributes such as an address, use code, and number of square feet.
-
Review the values of the
Use
field. You will use this field to style vector tiles in your application.Type
Get the starter app
Select a type of authentication below and follow the steps to create a new application.
Set up authentication
Create developer credentials in your portal for the type of authentication you selected.
Set developer credentials
Use the API key or OAuth developer credentials so your application can access location services.
Add a vector tile source
Use a source of type vector
to reference the Santa Monica Mountains Parcels vector tile service in your application.
-
Add an event handler to the map
load
event.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_URL", // The redirect URL registered in your OAuth credentials // portal: "YOUR_PORTAL_URL" // Your portal URL // }) // const accessToken = session.token; const basemapEnum = "arcgis/human-geography"; const map = new maplibregl.Map({ container: "map", // the id of the div element style: `https://basemapstyles-api.arcgis.com/arcgis/rest/services/styles/v2/styles/${basemapEnum}?token=${accessToken}`, zoom: 13, // starting zoom center: [-118.4877, 34.0227] // starting location [longitude, latitude] }); map.once("load", () => { }); // Add Esri attribution // Learn more in https://esriurl.com/attribution map._controls[0].options.customAttribution += " | Powered by Esri " map._controls[0]._updateAttributions() </script>
-
Inside the load event handler, add a vector tile source with id
parcels
. The source tells MapLibre GL JS how to access the data for the layer, but does not visually add it to the map.Use dark colors for code blocks map.once("load", () => { map.addSource("parcels", { type: "vector", tiles: [ "https://vectortileservices3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Santa_Monica_Mountains_Parcels_VTL/VectorTileServer/tile/{z}/{y}/{x}.pbf" ], }); map.moveLayer('parcels-outline'); });
-
Add the data attribution for the vector tile layer source.
- Go to the LA County Parcels item.
- Scroll down to the Credits (Attribution) section and copy its value.
- Create an
attribution
property and paste the attribution value from the item.Use dark colors for code blocks map.once("load", () => { map.addSource("parcels", { type: "vector", tiles: [ "https://vectortileservices3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Santa_Monica_Mountains_Parcels_VTL/VectorTileServer/tile/{z}/{y}/{x}.pbf" ], // Attribution text retrieved from https://arcgis.com/home/item.html?id=f0298e881b5b4743bbdf2c7d378acc84 attribution: 'County of Los Angeles Office of the Assessor' }); map.moveLayer('parcels-outline'); });
Style parcels by use type
Use a layer of type fill
to style land parcels according to their Use
category.
-
Add a
fill
layer that references theparcels
source you created. Setsource-layer
toSanta
to reference the original feature service._Monica _Mountains _Parcels Use dark colors for code blocks map.once("load", () => { map.addSource("parcels", { type: "vector", tiles: [ "https://vectortileservices3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Santa_Monica_Mountains_Parcels_VTL/VectorTileServer/tile/{z}/{y}/{x}.pbf" ], // Attribution text retrieved from https://arcgis.com/home/item.html?id=f0298e881b5b4743bbdf2c7d378acc84 attribution: 'County of Los Angeles Office of the Assessor' }); map.addLayer({ id: "parcels-fill", type: "fill", source: "parcels", "source-layer": "Santa_Monica_Mountains_Parcels", }); });
-
Add a custom
fill-color
to the layer. Useget
to retrieveUse
attribute values, viewed in the previous step. UseType case
to create a conditional expression that assigns a unique color to each use type.Use dark colors for code blocks map.addLayer({ id: "parcels-fill", type: "fill", source: "parcels", "source-layer": "Santa_Monica_Mountains_Parcels", paint: { "fill-color": ['case', ['==', ['get','UseType'], 'Residential'], '#E8E191', // Yellow ['==', ['get','UseType'], 'Commercial'], '#E580A2', // Red ['==', ['get','UseType'], 'Government'], '#79E284', // Green ['==', ['get','UseType'], 'Industrial'], '#C080E5', // Purple ['==', ['get','UseType'], 'Institutional'], '#80BBE5', // Blue '#bfbfbf' ] } });
Add an outline layer
The MapLibre style specification has limited options for customizing polygon outlines. Add a second vector tile layer to customize the parcel outlines.
-
Add a
line
layer that also references theparcels
source. Style the color, width, and opacity of the parcel outlines.Use dark colors for code blocks paint: { "fill-color": ['case', ['==', ['get','UseType'], 'Residential'], '#E8E191', // Yellow ['==', ['get','UseType'], 'Commercial'], '#E580A2', // Red ['==', ['get','UseType'], 'Government'], '#79E284', // Green ['==', ['get','UseType'], 'Industrial'], '#C080E5', // Purple ['==', ['get','UseType'], 'Institutional'], '#80BBE5', // Blue '#bfbfbf' ] } }); map.addLayer({ id: "parcels-outline", type: "line", source: "parcels", "source-layer": "Santa_Monica_Mountains_Parcels", paint: { 'line-color':'#000000', 'line-width':0.25, 'line-opacity':0.25 } });
-
Use
move
to display the outline layer on top of the fill layer.Layer Use dark colors for code blocks paint: { "fill-color": ['case', ['==', ['get','UseType'], 'Residential'], '#E8E191', // Yellow ['==', ['get','UseType'], 'Commercial'], '#E580A2', // Red ['==', ['get','UseType'], 'Government'], '#79E284', // Green ['==', ['get','UseType'], 'Industrial'], '#C080E5', // Purple ['==', ['get','UseType'], 'Institutional'], '#80BBE5', // Blue '#bfbfbf' ] } }); map.addLayer({ id: "parcels-outline", type: "line", source: "parcels", "source-layer": "Santa_Monica_Mountains_Parcels", paint: { 'line-color':'#000000', 'line-width':0.25, 'line-opacity':0.25 } }); map.moveLayer('parcels-outline');
Run the app
Run the app.
You should see the styled vector tile layer with parcels displayed on the basemap layer. Parcels should be styled in different colors based on their use type, and thin outlines should be present around each parcel.
What's next?
Learn how to use additional ArcGIS location services in these tutorials: