Skip to content
Show Polygon Information on Click

Show Polygon Information on Click in MapLibre GL JS

On this page

Showing feature data on polygon click requires listening to the map’s click event on a fill layer and reading the properties of the first returned feature. A Popup is then placed at the click coordinates with the feature properties formatted as HTML. This example loads Austria’s federal-state boundaries from a public GeoJSON dataset, and shows each state’s name, capital and area on click. Use this pattern for choropleth maps, administrative boundary explorers, or any map where polygon features carry data that users should be able to inspect by clicking.

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.35, 47.6],
        zoom: 6.4,
        attributionControl: { compact: false }
    });

    map.addControl(new maplibregl.NavigationControl(), 'top-right');

    // The capital of each of Austria's nine federal states, keyed by the state code in the dataset.
    const CAPITALS = {
        1: 'Eisenstadt', 2: 'Klagenfurt am Wörthersee', 3: 'St. Pölten',
        4: 'Linz', 5: 'Salzburg', 6: 'Graz',
        7: 'Innsbruck', 8: 'Bregenz', 9: 'Wien'
    };

    // Geodesic area (m^2) for Polygon / MultiPolygon rings (spherical excess, like turf.area).
    function ringArea(ring) {
        const R = 6378137;
        let total = 0;
        for (let i = 0; i < ring.length - 1; i++) {
            const [lng1, lat1] = ring[i];
            const [lng2, lat2] = ring[i + 1];
            total += (lng2 - lng1) * Math.PI / 180 *
                (2 + Math.sin(lat1 * Math.PI / 180) + Math.sin(lat2 * Math.PI / 180));
        }
        return Math.abs(total * R * R / 2);
    }
    function polygonArea(rings) {
        let area = ringArea(rings[0]);
        for (let i = 1; i < rings.length; i++) area -= ringArea(rings[i]); // subtract holes
        return area;
    }
    function featureArea(geometry) {
        if (geometry.type === 'MultiPolygon') {
            return geometry.coordinates.reduce((sum, poly) => sum + polygonArea(poly), 0);
        }
        return polygonArea(geometry.coordinates);
    }

    map.on('load', async () => {
        // Administrative boundaries: public dataset (Statistik Austria, via the
        // ginseng666/GeoJSON-TopoJSON-Austria project), served over the jsDelivr CDN.
        const response = await fetch('https://cdn.jsdelivr.net/gh/ginseng666/GeoJSON-TopoJSON-Austria@master/2021/simplified-99.9/laender_999_geo.json');
        const states = await response.json();

        // Give each state its capital and its area.
        states.features.forEach((feature) => {
            feature.properties.capital = CAPITALS[feature.properties.iso];
            feature.properties.area = Math.round(featureArea(feature.geometry) / 1e6).toLocaleString() + ' km²';
        });

        map.addSource('states', { type: 'geojson', data: states });

        // Add the states below the first label layer, so the map labels stay readable.
        const firstSymbolId = map.getStyle().layers.find((layer) => layer.type === 'symbol')?.id;

        map.addLayer({
            id: 'states-fill',
            type: 'fill',
            source: 'states',
            paint: {
                'fill-color': '#2c7fb8',
                'fill-opacity': 0.45
            }
        }, firstSymbolId);

        map.addLayer({
            id: 'states-border',
            type: 'line',
            source: 'states',
            paint: { 'line-color': '#ffffff', 'line-width': 1 }
        }, firstSymbolId);

        map.fitBounds([[9.45, 46.35], [17.2, 49.1]], { padding: 30, duration: 0 });

        map.on('click', 'states-fill', (e) => {
            const p = e.features[0].properties;
            new maplibregl.Popup()
                .setLngLat(e.lngLat)
                .setHTML(`<strong>${p.name}</strong><br>Capital: ${p.capital}<br>Area: ${p.area}`)
                .addTo(map);
        });

        map.on('mouseenter', 'states-fill', () => { map.getCanvas().style.cursor = 'pointer'; });
        map.on('mouseleave', 'states-fill', () => { map.getCanvas().style.cursor = ''; });
    });

    map.addControl(new LogoControl({ variant: "com" }));
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Show Polygon Information on Click - MapLibre GL JS</title>
    <meta property="og:description" content="Show a popup with feature properties when clicking on a polygon." />
    <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.35, 47.6],
        zoom: 6.4,
        attributionControl: { compact: false }
    });

    map.addControl(new maplibregl.NavigationControl(), 'top-right');

    // The capital of each of Austria's nine federal states, keyed by the state code in the dataset.
    const CAPITALS = {
        1: 'Eisenstadt', 2: 'Klagenfurt am Wörthersee', 3: 'St. Pölten',
        4: 'Linz', 5: 'Salzburg', 6: 'Graz',
        7: 'Innsbruck', 8: 'Bregenz', 9: 'Wien'
    };

    // Geodesic area (m^2) for Polygon / MultiPolygon rings (spherical excess, like turf.area).
    function ringArea(ring) {
        const R = 6378137;
        let total = 0;
        for (let i = 0; i < ring.length - 1; i++) {
            const [lng1, lat1] = ring[i];
            const [lng2, lat2] = ring[i + 1];
            total += (lng2 - lng1) * Math.PI / 180 *
                (2 + Math.sin(lat1 * Math.PI / 180) + Math.sin(lat2 * Math.PI / 180));
        }
        return Math.abs(total * R * R / 2);
    }
    function polygonArea(rings) {
        let area = ringArea(rings[0]);
        for (let i = 1; i < rings.length; i++) area -= ringArea(rings[i]); // subtract holes
        return area;
    }
    function featureArea(geometry) {
        if (geometry.type === 'MultiPolygon') {
            return geometry.coordinates.reduce((sum, poly) => sum + polygonArea(poly), 0);
        }
        return polygonArea(geometry.coordinates);
    }

    map.on('load', async () => {
        // Administrative boundaries: public dataset (Statistik Austria, via the
        // ginseng666/GeoJSON-TopoJSON-Austria project), served over the jsDelivr CDN.
        const response = await fetch('https://cdn.jsdelivr.net/gh/ginseng666/GeoJSON-TopoJSON-Austria@master/2021/simplified-99.9/laender_999_geo.json');
        const states = await response.json();

        // Give each state its capital and its area.
        states.features.forEach((feature) => {
            feature.properties.capital = CAPITALS[feature.properties.iso];
            feature.properties.area = Math.round(featureArea(feature.geometry) / 1e6).toLocaleString() + ' km²';
        });

        map.addSource('states', { type: 'geojson', data: states });

        // Add the states below the first label layer, so the map labels stay readable.
        const firstSymbolId = map.getStyle().layers.find((layer) => layer.type === 'symbol')?.id;

        map.addLayer({
            id: 'states-fill',
            type: 'fill',
            source: 'states',
            paint: {
                'fill-color': '#2c7fb8',
                'fill-opacity': 0.45
            }
        }, firstSymbolId);

        map.addLayer({
            id: 'states-border',
            type: 'line',
            source: 'states',
            paint: { 'line-color': '#ffffff', 'line-width': 1 }
        }, firstSymbolId);

        map.fitBounds([[9.45, 46.35], [17.2, 49.1]], { padding: 30, duration: 0 });

        map.on('click', 'states-fill', (e) => {
            const p = e.features[0].properties;
            new maplibregl.Popup()
                .setLngLat(e.lngLat)
                .setHTML(`<strong>${p.name}</strong><br>Capital: ${p.capital}<br>Area: ${p.area}`)
                .addTo(map);
        });

        map.on('mouseenter', 'states-fill', () => { map.getCanvas().style.cursor = 'pointer'; });
        map.on('mouseleave', 'states-fill', () => { map.getCanvas().style.cursor = ''; });
    });

    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.

Use the Maptoolkit MCP. Create an interactive map centered on Austria. Load the Austrian federal-state boundaries from a public GeoJSON dataset, fill them in one color, and show a popup with the state’s name, capital and area on click.

How it works

The click handler is registered against the fill layer, so e.features contains only polygons from that layer and e.features[0].properties is the data behind the one clicked.

The popup is placed at e.lngLat, the click position, rather than at the polygon’s center. For a large or awkwardly shaped state a centroid can fall outside the polygon entirely, so the click point is usually the better anchor.

The area calculation is worth noticing: it is geodesic, computed on the sphere with the same spherical-excess approach as turf.area, not a planar area from the raw coordinates. A planar calculation over degrees is wrong by a factor that grows with latitude, so Austria would come out noticeably too large.

properties values arrive as strings when they came from vector tiles, so numbers usually need coercing before formatting.

Next steps

One polygon’s data is a start; comparing them is usually the point. Coloring the fill from the same property you are reading turns the map into a choropleth, so the pattern is visible before anyone clicks.

Joining the tile features to your own data by name or code is the step after that, and it is where most of the work lands: the map supplies the shapes, your database supplies the numbers, and Connectors are how the two meet once the join is too large for the browser.