Skip to content

Hover Effect

Use setFeatureState plus a feature-state expression to highlight whichever polygon is under the pointer, without re-styling the whole layer.

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: [6.5, 48.7], zoom: 4.5 });
const fc = { type: 'FeatureCollection', features: [
  { type: 'Feature', properties: {}, geometry: { type: 'Polygon', coordinates: [[[11, 49], [12.5, 49], [12.5, 48], [11, 48], [11, 49]]] } },
  { type: 'Feature', properties: {}, geometry: { type: 'Polygon', coordinates: [[[2, 49], [3, 49], [3, 48.5], [2, 48.5], [2, 49]]] } }
] };
map.on('load', function () {
  map.addSource('areas', { type: 'geojson', data: fc, generateId: true });
  map.addLayer({ id: 'areas', type: 'fill', source: 'areas', paint: { 'fill-color': '#1a73e8', 'fill-opacity': ['case', ['boolean', ['feature-state', 'hover'], false], 0.8, 0.3] } });
  let hovered = null;
  map.on('mousemove', 'areas', function (e) {
    if (!e.features.length) return;
    if (hovered !== null) map.setFeatureState({ source: 'areas', id: hovered }, { hover: false });
    hovered = e.features[0].id;
    map.setFeatureState({ source: 'areas', id: hovered }, { hover: true });
  });
  map.on('mouseleave', 'areas', function () {
    if (hovered !== null) map.setFeatureState({ source: 'areas', id: hovered }, { hover: false });
    hovered = null;
  });
});
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: [6.5, 48.7], zoom: 4.5 });
const fc = { type: 'FeatureCollection', features: [
  { type: 'Feature', properties: {}, geometry: { type: 'Polygon', coordinates: [[[11, 49], [12.5, 49], [12.5, 48], [11, 48], [11, 49]]] } },
  { type: 'Feature', properties: {}, geometry: { type: 'Polygon', coordinates: [[[2, 49], [3, 49], [3, 48.5], [2, 48.5], [2, 49]]] } }
] };
map.on('load', function () {
  map.addSource('areas', { type: 'geojson', data: fc, generateId: true });
  map.addLayer({ id: 'areas', type: 'fill', source: 'areas', paint: { 'fill-color': '#1a73e8', 'fill-opacity': ['case', ['boolean', ['feature-state', 'hover'], false], 0.8, 0.3] } });
  let hovered = null;
  map.on('mousemove', 'areas', function (e) {
    if (!e.features.length) return;
    if (hovered !== null) map.setFeatureState({ source: 'areas', id: hovered }, { hover: false });
    hovered = e.features[0].id;
    map.setFeatureState({ source: 'areas', id: hovered }, { hover: true });
  });
  map.on('mouseleave', 'areas', function () {
    if (hovered !== null) map.setFeatureState({ source: 'areas', id: hovered }, { hover: false });
    hovered = null;
  });
});
map.addControl(new MaptoolkitLogoControl());
  </script>
</body>
</html>