Skip to content

Popup on Click

Add a GeoJSON circle layer and bind a click handler to it. The handler reads the clicked feature and opens a maplibregl.Popup at its coordinates. The pointer cursor changes on hover for affordance.

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: [5, 48], zoom: 3.3 });
const cities = {
  type: 'FeatureCollection',
  features: [
    { type: 'Feature', properties: { name: 'Berlin' }, geometry: { type: 'Point', coordinates: [13.405, 52.52] } },
    { type: 'Feature', properties: { name: 'Paris' }, geometry: { type: 'Point', coordinates: [2.3522, 48.8566] } },
    { type: 'Feature', properties: { name: 'London' }, geometry: { type: 'Point', coordinates: [-0.1276, 51.5074] } },
    { type: 'Feature', properties: { name: 'Madrid' }, geometry: { type: 'Point', coordinates: [-3.7038, 40.4168] } },
    { type: 'Feature', properties: { name: 'Rome' }, geometry: { type: 'Point', coordinates: [12.4964, 41.9028] } }
  ]
};
map.on('load', function () {
  map.addSource('cities', { type: 'geojson', data: cities });
  map.addLayer({ id: 'cities', type: 'circle', source: 'cities', paint: { 'circle-radius': 8, 'circle-color': '#1a73e8', 'circle-stroke-width': 2, 'circle-stroke-color': '#fff' } });
  map.on('click', 'cities', function (e) {
    const f = e.features[0];
    new maplibregl.Popup().setLngLat(f.geometry.coordinates).setHTML('<strong>' + f.properties.name + '</strong>').addTo(map);
  });
  map.on('mouseenter', 'cities', function () { map.getCanvas().style.cursor = 'pointer'; });
  map.on('mouseleave', 'cities', function () { map.getCanvas().style.cursor = ''; });
});
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: [5, 48], zoom: 3.3 });
const cities = {
  type: 'FeatureCollection',
  features: [
    { type: 'Feature', properties: { name: 'Berlin' }, geometry: { type: 'Point', coordinates: [13.405, 52.52] } },
    { type: 'Feature', properties: { name: 'Paris' }, geometry: { type: 'Point', coordinates: [2.3522, 48.8566] } },
    { type: 'Feature', properties: { name: 'London' }, geometry: { type: 'Point', coordinates: [-0.1276, 51.5074] } },
    { type: 'Feature', properties: { name: 'Madrid' }, geometry: { type: 'Point', coordinates: [-3.7038, 40.4168] } },
    { type: 'Feature', properties: { name: 'Rome' }, geometry: { type: 'Point', coordinates: [12.4964, 41.9028] } }
  ]
};
map.on('load', function () {
  map.addSource('cities', { type: 'geojson', data: cities });
  map.addLayer({ id: 'cities', type: 'circle', source: 'cities', paint: { 'circle-radius': 8, 'circle-color': '#1a73e8', 'circle-stroke-width': 2, 'circle-stroke-color': '#fff' } });
  map.on('click', 'cities', function (e) {
    const f = e.features[0];
    new maplibregl.Popup().setLngLat(f.geometry.coordinates).setHTML('<strong>' + f.properties.name + '</strong>').addTo(map);
  });
  map.on('mouseenter', 'cities', function () { map.getCanvas().style.cursor = 'pointer'; });
  map.on('mouseleave', 'cities', function () { map.getCanvas().style.cursor = ''; });
});
map.addControl(new MaptoolkitLogoControl());
  </script>
</body>
</html>