Skip to content
Display a Popup on Hover

Display a Popup on Hover

Display a popup when hovering over a point feature on the map.

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: 7,
        attributionControl: { compact: false }
    });

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

    const popup = new maptoolkit.Popup({ closeButton: false, closeOnClick: false });

    const cities = {
        type: 'FeatureCollection',
        features: [
            { type: 'Feature', geometry: { type: 'Point', coordinates: [11.39085, 47.27574] }, properties: { name: 'Innsbruck', country: 'Austria' } },
            { type: 'Feature', geometry: { type: 'Point', coordinates: [16.3738, 48.2082] }, properties: { name: 'Vienna', country: 'Austria' } },
            { type: 'Feature', geometry: { type: 'Point', coordinates: [13.0550, 47.8095] }, properties: { name: 'Salzburg', country: 'Austria' } },
            { type: 'Feature', geometry: { type: 'Point', coordinates: [15.4395, 47.0707] }, properties: { name: 'Graz', country: 'Austria' } },
            { type: 'Feature', geometry: { type: 'Point', coordinates: [11.576, 48.1374] }, properties: { name: 'Munich', country: 'Germany' } },
        ]
    };

    map.on('load', () => {
        map.addSource('cities', { type: 'geojson', data: cities });
        map.addLayer({
            id: 'cities',
            type: 'circle',
            source: 'cities',
            paint: { 'circle-radius': 8, 'circle-color': '#3887be' }
        });

        map.on('mouseenter', 'cities', (e) => {
            map.getCanvas().style.cursor = 'pointer';
            const coordinates = e.features[0].geometry.coordinates.slice();
            const { name, country } = e.features[0].properties;
            popup.setLngLat(coordinates).setHTML(`<strong>${name}</strong><br>${country}`).addTo(map);
        });

        map.on('mouseleave', 'cities', () => {
            map.getCanvas().style.cursor = '';
            popup.remove();
        });
    });
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Display a Popup on Hover – Maptoolkit Maps JS</title>
    <meta property="og:description" content="Display a popup when hovering over a point feature on the map." />
    <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: 7,
        attributionControl: { compact: false }
    });

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

    const popup = new maptoolkit.Popup({ closeButton: false, closeOnClick: false });

    const cities = {
        type: 'FeatureCollection',
        features: [
            { type: 'Feature', geometry: { type: 'Point', coordinates: [11.39085, 47.27574] }, properties: { name: 'Innsbruck', country: 'Austria' } },
            { type: 'Feature', geometry: { type: 'Point', coordinates: [16.3738, 48.2082] }, properties: { name: 'Vienna', country: 'Austria' } },
            { type: 'Feature', geometry: { type: 'Point', coordinates: [13.0550, 47.8095] }, properties: { name: 'Salzburg', country: 'Austria' } },
            { type: 'Feature', geometry: { type: 'Point', coordinates: [15.4395, 47.0707] }, properties: { name: 'Graz', country: 'Austria' } },
            { type: 'Feature', geometry: { type: 'Point', coordinates: [11.576, 48.1374] }, properties: { name: 'Munich', country: 'Germany' } },
        ]
    };

    map.on('load', () => {
        map.addSource('cities', { type: 'geojson', data: cities });
        map.addLayer({
            id: 'cities',
            type: 'circle',
            source: 'cities',
            paint: { 'circle-radius': 8, 'circle-color': '#3887be' }
        });

        map.on('mouseenter', 'cities', (e) => {
            map.getCanvas().style.cursor = 'pointer';
            const coordinates = e.features[0].geometry.coordinates.slice();
            const { name, country } = e.features[0].properties;
            popup.setLngLat(coordinates).setHTML(`<strong>${name}</strong><br>${country}`).addTo(map);
        });

        map.on('mouseleave', 'cities', () => {
            map.getCanvas().style.cursor = '';
            popup.remove();
        });
    });
</script>
</body>
</html>