Add a Layer Below Labels
Pass a beforeId to map.addLayer() to control stacking. Finding the first symbol layer keeps your fill below all text labels.
import { MaptoolkitLogoControl } from 'https://cdn.jsdelivr.net/npm/@maptoolkit/maplibre-gl-logo/+esm';
const map = new maplibregl.Map({ container: 'map', attributionControl: { customAttribution: '' }, style: 'https://styles.maptoolkit.org/summer.json', center: [2.345, 48.862], zoom: 11 }); // central Paris
const area = { type: 'Feature', properties: {}, geometry: { type: 'Polygon', coordinates: [[[2.30, 48.88], [2.39, 48.88], [2.39, 48.84], [2.30, 48.84], [2.30, 48.88]]] } };
map.on('load', function () {
const layers = map.getStyle().layers;
let firstSymbol;
for (let i = 0; i < layers.length; i++) { if (layers[i].type === 'symbol') { firstSymbol = layers[i].id; break; } }
map.addSource('area', { type: 'geojson', data: area });
map.addLayer({ id: 'area', type: 'fill', source: 'area', paint: { 'fill-color': '#e8551a', 'fill-opacity': 0.5 } }, firstSymbol);
});
map.addControl(new MaptoolkitLogoControl());<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/maplibre-gl.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/maplibre-gl.css" />
<style>
html, body { width: 100%; height: 100%; margin: 0; padding: 0; }
#map { width: 100%; height: 100%; }
</style>
</head>
<body>
<div id="map"></div>
<script type="module">
import { MaptoolkitLogoControl } from 'https://cdn.jsdelivr.net/npm/@maptoolkit/maplibre-gl-logo/+esm';
const map = new maplibregl.Map({ container: 'map', attributionControl: { customAttribution: '' }, style: 'https://styles.maptoolkit.org/summer.json', center: [2.345, 48.862], zoom: 11 }); // central Paris
const area = { type: 'Feature', properties: {}, geometry: { type: 'Polygon', coordinates: [[[2.30, 48.88], [2.39, 48.88], [2.39, 48.84], [2.30, 48.84], [2.30, 48.88]]] } };
map.on('load', function () {
const layers = map.getStyle().layers;
let firstSymbol;
for (let i = 0; i < layers.length; i++) { if (layers[i].type === 'symbol') { firstSymbol = layers[i].id; break; } }
map.addSource('area', { type: 'geojson', data: area });
map.addLayer({ id: 'area', type: 'fill', source: 'area', paint: { 'fill-color': '#e8551a', 'fill-opacity': 0.5 } }, firstSymbol);
});
map.addControl(new MaptoolkitLogoControl());
</script>
</body>
</html>