Add a WMS Source in MapLibre GL JS
On this page
WMS (Web Map Service) is a standard protocol for map images. Government and scientific data providers often publish their data this way. This example adds the Natura 2000 protected areas, published by the European Environment Agency (EEA), as a transparent layer on a Maptoolkit vector map.
import * as maplibregl from "maplibre-gl";
import { LogoControl } from "https://cdn.jsdelivr.net/npm/@maptoolkit/maplibre-logo-control@1.1.2/dist/maplibre-logo-control.js";
const API_KEY = 'YOUR_API_KEY';
const map = new maplibregl.Map({
container: 'map',
style: `https://styles.maptoolkit.net/maptoolkit/maptoolkit.summer.json?api_key=${API_KEY}`,
center: [13.0, 47.5],
zoom: 7,
attributionControl: { compact: false }
});
map.addControl(new maplibregl.NavigationControl(), 'top-right');
map.on('load', () => {
const firstSymbolId = map.getStyle().layers.find((layer) => layer.type === 'symbol')?.id;
map.addSource('natura2000', {
type: 'raster',
tiles: [
'https://bio.discomap.eea.europa.eu/arcgis/services/ProtectedSites/Natura2000Sites/MapServer/WMSServer?SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&FORMAT=image/png&TRANSPARENT=true&LAYERS=1,2&STYLES=&SRS=EPSG:3857&WIDTH=256&HEIGHT=256&BBOX={bbox-epsg-3857}'
],
tileSize: 256,
attribution: 'Natura 2000 © European Commission (DG ENV), <a href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a>'
});
map.addLayer({
id: 'natura2000',
type: 'raster',
source: 'natura2000',
paint: { 'raster-opacity': 0.7 }
}, firstSymbolId);
});
map.addControl(new LogoControl({ variant: "com" }));<!DOCTYPE html>
<html lang="en">
<head>
<title>Add a WMS Source - MapLibre GL JS</title>
<meta property="og:description" content="Add a WMS (Web Map Service) raster source to the map." />
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- MapLibre GL JS 6 is an ES module. The import map tells the browser
where to load "maplibre-gl" from. -->
<script type="importmap">
{
"imports": {
"maplibre-gl": "https://cdn.jsdelivr.net/npm/maplibre-gl@6.16.0/dist/maplibre-gl.mjs"
}
}
</script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/maplibre-gl@6.16.0/dist/maplibre-gl.css" />
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@maptoolkit/maplibre-logo-control@1.1.2/dist/maplibre-logo-control.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 * as maplibregl from "maplibre-gl";
import { LogoControl } from "https://cdn.jsdelivr.net/npm/@maptoolkit/maplibre-logo-control@1.1.2/dist/maplibre-logo-control.js";
const API_KEY = 'YOUR_API_KEY';
const map = new maplibregl.Map({
container: 'map',
style: `https://styles.maptoolkit.net/maptoolkit/maptoolkit.summer.json?api_key=${API_KEY}`,
center: [13.0, 47.5],
zoom: 7,
attributionControl: { compact: false }
});
map.addControl(new maplibregl.NavigationControl(), 'top-right');
map.on('load', () => {
const firstSymbolId = map.getStyle().layers.find((layer) => layer.type === 'symbol')?.id;
map.addSource('natura2000', {
type: 'raster',
tiles: [
'https://bio.discomap.eea.europa.eu/arcgis/services/ProtectedSites/Natura2000Sites/MapServer/WMSServer?SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&FORMAT=image/png&TRANSPARENT=true&LAYERS=1,2&STYLES=&SRS=EPSG:3857&WIDTH=256&HEIGHT=256&BBOX={bbox-epsg-3857}'
],
tileSize: 256,
attribution: 'Natura 2000 © European Commission (DG ENV), <a href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a>'
});
map.addLayer({
id: 'natura2000',
type: 'raster',
source: 'natura2000',
paint: { 'raster-opacity': 0.7 }
}, firstSymbolId);
});
map.addControl(new LogoControl({ variant: "com" }));
</script>
</body>
</html>Use the prompt below with any LLM to get the same result. Make sure the Maptoolkit MCP server is connected first — check out AI Integration & MCP to get started.
How it works
A WMS server does not use tiles. It returns one image for any bounding box you request.
MapLibre GL JS connects the two with a raster source: the tile URL contains the
{bbox-epsg-3857} placeholder, and MapLibre GL JS replaces it with the extent of each tile.
The other URL parameters are defined by the WMS standard:
| Parameter | Value in this example | Why |
|---|---|---|
LAYERS | 1,2 | The layers of the service to draw. Here: bird protection areas (1) and habitat protection areas (2). The GetCapabilities request of a service lists its layers. |
SRS | EPSG:3857 | Must be Web Mercator, the projection of the map. With another projection, the images do not line up with the map. WMS 1.3.0 uses CRS instead of SRS. |
WIDTH, HEIGHT | 256 | Must match tileSize. |
FORMAT, TRANSPARENT | image/png, true | A transparent PNG shows only the protected areas, so the map stays visible below them. |
As with any raster source, the attribution option adds the data credit to the attribution
control. The layer is added below the first symbol layer, so labels stay on top, and
raster-opacity keeps the roads and terrain below the areas visible.
A WMS server renders each image on request, so it is usually slower than a tile service.
Next steps
- Add an opacity slider and a layer toggle so users can fade the overlay or turn it off.
- For services that provide XYZ tiles instead of WMS, see Add a Raster Tile Source.