Skip to content
Display a Popup on Click

Display a Popup on Click

Showing a popup on any map click requires listening to the map’s click event and using the event’s lngLat property to position a new Popup at the cursor’s geographic coordinates. The popup is removed before a new one is created so only one is visible at a time. Use this pattern as the foundation for coordinate pickers, location inspectors, or any tool where clicking the map should reveal information about that point.

const API_KEY = 'YOUR_API_KEY';

    const map = new maptoolkit.Map({
        container: 'map',
        apiKey: API_KEY,
        style: `https://styles.maptoolkit.net/maptoolkit/maptoolkit.summer.json?api_key=${API_KEY}`,
        center: [11.39085, 47.27574],
        zoom: 12,
        attributionControl: { compact: false }
    });

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

    map.on('click', (e) => {
        new maptoolkit.Popup()
            .setLngLat(e.lngLat)
            .setHTML(`<strong>Coordinates:</strong><br>${e.lngLat.lng.toFixed(5)}, ${e.lngLat.lat.toFixed(5)}`)
            .addTo(map);
    });
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Display a Popup on Click - Maptoolkit Maps JS</title>
    <meta property="og:description" content="Display a popup with coordinates when clicking anywhere on the map." />
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script src="https://unpkg.com/@maptoolkit/[email protected]/dist/maptoolkit.js"></script>
    <link rel="stylesheet" href="https://unpkg.com/@maptoolkit/[email protected]/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 = 'YOUR_API_KEY';

    const map = new maptoolkit.Map({
        container: 'map',
        apiKey: API_KEY,
        style: `https://styles.maptoolkit.net/maptoolkit/maptoolkit.summer.json?api_key=${API_KEY}`,
        center: [11.39085, 47.27574],
        zoom: 12,
        attributionControl: { compact: false }
    });

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

    map.on('click', (e) => {
        new maptoolkit.Popup()
            .setLngLat(e.lngLat)
            .setHTML(`<strong>Coordinates:</strong><br>${e.lngLat.lng.toFixed(5)}, ${e.lngLat.lat.toFixed(5)}`)
            .addTo(map);
    });
</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 Connector. Create an interactive map with zoom level 12, centered around [11.39085, 47.27574]. Clicking anywhere on the map should display a popup showing the clicked coordinates.