Skip to content
Show Polygon Information on Click

Show Polygon Information on Click

Show a popup with feature properties when clicking on a polygon.

const API_KEY = 'toursprung';

    const map = new maptoolkit.Map({
        container: 'map',
        style: `https://static.maptoolkit.net/styles/toursprung/terrain.json?api_key=${API_KEY}`,
        center: [12.0, 47.5],
        zoom: 6,
        attributionControl: { compact: false }
    });

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

    const regions = {
        type: 'FeatureCollection',
        features: [
            {
                type: 'Feature',
                geometry: { type: 'Polygon', coordinates: [[[9.5, 46.4], [13.2, 46.4], [13.2, 48.0], [9.5, 48.0], [9.5, 46.4]]] },
                properties: { name: 'Western Austria', area: '~43,000 km²', capital: 'Innsbruck' }
            },
            {
                type: 'Feature',
                geometry: { type: 'Polygon', coordinates: [[[13.2, 46.4], [17.2, 46.4], [17.2, 49.1], [13.2, 49.1], [13.2, 46.4]]] },
                properties: { name: 'Eastern Austria', area: '~41,000 km²', capital: 'Vienna' }
            }
        ]
    };

    map.on('load', () => {
        map.addSource('regions', { type: 'geojson', data: regions });

        map.addLayer({ id: 'regions-fill', type: 'fill', source: 'regions',
            paint: { 'fill-color': '#3887be', 'fill-opacity': 0.3 }
        });

        map.addLayer({ id: 'regions-border', type: 'line', source: 'regions',
            paint: { 'line-color': '#3887be', 'line-width': 2 }
        });

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

        map.on('mouseenter', 'regions-fill', () => { map.getCanvas().style.cursor = 'pointer'; });
        map.on('mouseleave', 'regions-fill', () => { map.getCanvas().style.cursor = ''; });
    });
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Show Polygon Information on Click – Maptoolkit Maps 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">
    <script src="https://unpkg.com/@maptoolkit/maps@11.0.0-beta.2/dist/maptoolkit.js"></script>
    <link rel="stylesheet" href="https://unpkg.com/@maptoolkit/maps@11.0.0-beta.2/dist/maptoolkit.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>
    const API_KEY = 'toursprung';

    const map = new maptoolkit.Map({
        container: 'map',
        style: `https://static.maptoolkit.net/styles/toursprung/terrain.json?api_key=${API_KEY}`,
        center: [12.0, 47.5],
        zoom: 6,
        attributionControl: { compact: false }
    });

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

    const regions = {
        type: 'FeatureCollection',
        features: [
            {
                type: 'Feature',
                geometry: { type: 'Polygon', coordinates: [[[9.5, 46.4], [13.2, 46.4], [13.2, 48.0], [9.5, 48.0], [9.5, 46.4]]] },
                properties: { name: 'Western Austria', area: '~43,000 km²', capital: 'Innsbruck' }
            },
            {
                type: 'Feature',
                geometry: { type: 'Polygon', coordinates: [[[13.2, 46.4], [17.2, 46.4], [17.2, 49.1], [13.2, 49.1], [13.2, 46.4]]] },
                properties: { name: 'Eastern Austria', area: '~41,000 km²', capital: 'Vienna' }
            }
        ]
    };

    map.on('load', () => {
        map.addSource('regions', { type: 'geojson', data: regions });

        map.addLayer({ id: 'regions-fill', type: 'fill', source: 'regions',
            paint: { 'fill-color': '#3887be', 'fill-opacity': 0.3 }
        });

        map.addLayer({ id: 'regions-border', type: 'line', source: 'regions',
            paint: { 'line-color': '#3887be', 'line-width': 2 }
        });

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

        map.on('mouseenter', 'regions-fill', () => { map.getCanvas().style.cursor = 'pointer'; });
        map.on('mouseleave', 'regions-fill', () => { map.getCanvas().style.cursor = ''; });
    });
</script>
</body>
</html>