addLayer(
layer:AddLayerObject,beforeId?:string):this
Defined in: ui/map.ts:3541
Adds a MapLibre style layer to the map's style.
A layer defines how data from a specified source will be styled. Read more about layer types and available paint and layout properties in the MapLibre Style Specification.
Parameters
| Parameter | Type | Description |
|---|---|---|
layer |
AddLayerObject |
The layer to add, conforming to either the MapLibre Style Specification's layer definition or, less commonly, the CustomLayerInterface specification. Can also be a layer definition with an embedded source definition. The MapLibre Style Specification's layer definition is appropriate for most layers. |
beforeId? |
string |
The ID of an existing layer to insert the new layer before, resulting in the new layer appearing visually beneath the existing layer. If this argument is not specified, the layer will be appended to the end of the layers array and appear visually above all other layers. |
Returns
this
Examples
Add a circle layer with a vector source
map.addLayer({
id: 'points-of-interest',
source: {
type: 'vector',
url: 'https://demotiles.maplibre.org/tiles/tiles.json'
},
'source-layer': 'poi_label',
type: 'circle',
paint: {
// MapLibre Style Specification paint properties
},
layout: {
// MapLibre Style Specification layout properties
}
});
Define a source before using it to create a new layer
map.addSource('state-data', {
type: 'geojson',
data: 'path/to/data.geojson'
});
map.addLayer({
id: 'states',
// References the GeoJSON source defined above
// and does not require a `source-layer`
source: 'state-data',
type: 'symbol',
layout: {
// Set the label content to the
// feature's `name` property
text-field: ['get', 'name']
}
});
Add a new symbol layer before an existing layer
map.addLayer({
id: 'states',
// References a source that's already been defined
source: 'state-data',
type: 'symbol',
layout: {
// Set the label content to the
// feature's `name` property
text-field: ['get', 'name']
}
// Add the layer before the existing `cities` layer
}, 'cities');
See
- Create and style clusters
- Add a vector tile source
- Add a WMS source