Display a popup on hover
This example demonstrates how to create a Popup
when a user hovers over a circle.
Using the Interactions API, a mouseenter
listener is created to detect when the mouse begins to hover over a circle. The handler then grabs the underlying geojson features geometry & properties, including the circle's coordinates and a title and description. With this data, a pop up is rendered over the circle's coordinates by calling setLngLat(coordinates)
and setting the text of the pop up using the setHTML(description)
function.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Display a popup on hover</title>
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no">
<link href="https://api.mapbox.com/mapbox-gl-js/v3.15.0/mapbox-gl.css" rel="stylesheet">
<script src="https://api.mapbox.com/mapbox-gl-js/v3.15.0/mapbox-gl.js"></script>
<style>
body { margin: 0; padding: 0; }
#map { position: absolute; top: 0; bottom: 0; width: 100%; }
</style>
</head>
<body>
<style>
.mapboxgl-popup {
max-width: 400px;
font:
12px/20px 'Helvetica Neue',
Arial,
Helvetica,
sans-serif;
}
</style>
<div id="map"></div>
<script>
// TO MAKE THE MAP APPEAR YOU MUST
// ADD YOUR ACCESS TOKEN FROM
// https://account.mapbox.com
mapboxgl.accessToken = 'YOUR_MAPBOX_ACCESS_TOKEN';
const map = new mapboxgl.Map({
container: 'map',
// Choose from Mapbox's core styles, or make your own style with Mapbox Studio
style: 'mapbox://styles/mapbox/standard',
center: [-77.04, 38.907],
zoom: 11.15
});
map.on('load', () => {
map.addSource('places', {
'type': 'geojson',
'generateId': true,
'data': {
'type': 'FeatureCollection',
'features': [
{
'type': 'Feature',
'properties': {
'description':
'<strong>Muhsinah</strong><p>Jazz-influenced hip hop artist Muhsinah plays the Black Cat (1811 14th Street NW) tonight with Exit Clov and Gods’illa. 9:00 p.m. $12.</p>'
},
'geometry': {
'type': 'Point',
'coordinates': [-77.031706, 38.914581]
}
},
{
'type': 'Feature',
'properties': {
'description':
"<strong>A Little Night Music</strong><p>The Arlington Players' production of Stephen Sondheim's <em>A Little Night Music</em> comes to the Kogod Cradle at The Mead Center for American Theater (1101 6th Street SW) this weekend and next. 8:00 p.m.</p>"
},
'geometry': {
'type': 'Point',
'coordinates': [-77.020945, 38.878241]
}
},
{
'type': 'Feature',
'properties': {
'description':
'<strong>Truckeroo</strong><p>Truckeroo brings dozens of food trucks, live music, and games to half and M Street SE (across from Navy Yard Metro Station) today from 11:00 a.m. to 11:00 p.m.</p>'
},
'geometry': {
'type': 'Point',
'coordinates': [-77.007481, 38.876516]
}
}
]
}
});
// Add a layer showing the places.
map.addLayer({
'id': 'places',
'type': 'circle',
'source': 'places',
'paint': {
'circle-color': '#4264fb',
'circle-radius': 6,
'circle-stroke-width': 2,
'circle-stroke-color': '#ffffff'
}
});
// Create the popup UI, but don't add it to the map yet.
// You only want the UI to appear once the cursor is hovering over an element.
const popup = new mapboxgl.Popup({
closeButton: false,
closeOnClick: false
});
map.addInteraction('places-mouseenter-interaction', {
type: 'mouseenter',
target: { layerId: 'places' },
handler: (e) => {
map.getCanvas().style.cursor = 'pointer';
// Copy the coordinates from the POI underneath the cursor
const coordinates = e.feature.geometry.coordinates.slice();
const description = e.feature.properties.description;
// Populate the popup and set its coordinates based on the feature found.
popup.setLngLat(coordinates).setHTML(description).addTo(map);
}
});
map.addInteraction('places-mouseleave-interaction', {
type: 'mouseleave',
target: { layerId: 'places' },
handler: () => {
map.getCanvas().style.cursor = '';
popup.remove();
}
});
});
</script>
</body>
</html>
This code snippet will not work as expected until you replace
YOUR_MAPBOX_ACCESS_TOKEN
with an access token from your Mapbox account.import React, { useEffect, useRef } from 'react';
import mapboxgl from 'mapbox-gl';
import 'mapbox-gl/dist/mapbox-gl.css';
const MapboxExample = () => {
const mapContainerRef = useRef();
const mapRef = useRef();
useEffect(() => {
// TO MAKE THE MAP APPEAR YOU MUST
// ADD YOUR ACCESS TOKEN FROM
// https://account.mapbox.com
mapboxgl.accessToken = 'YOUR_MAPBOX_ACCESS_TOKEN';
mapRef.current = new mapboxgl.Map({
container: mapContainerRef.current,
style: 'mapbox://styles/mapbox/standard',
center: [-77.04, 38.907],
zoom: 11.15
});
mapRef.current.on('load', () => {
mapRef.current.addSource('places', {
type: 'geojson',
generateId: true,
data: {
type: 'FeatureCollection',
features: [
{
type: 'Feature',
properties: {
description:
'<strong>Muhsinah</strong><p>Jazz-influenced hip hop artist Muhsinah plays the Black Cat (1811 14th Street NW) tonight with Exit Clov and Gods’illa. 9:00 p.m. $12.</p>'
},
geometry: {
type: 'Point',
coordinates: [-77.031706, 38.914581]
}
},
{
type: 'Feature',
properties: {
description:
"<strong>A Little Night Music</strong><p>The Arlington Players' production of Stephen Sondheim's <em>A Little Night Music</em> comes to the Kogod Cradle at The Mead Center for American Theater (1101 6th Street SW) this weekend and next. 8:00 p.m.</p>"
},
geometry: {
type: 'Point',
coordinates: [-77.020945, 38.878241]
}
},
{
type: 'Feature',
properties: {
description:
'<strong>Truckeroo</strong><p>Truckeroo brings dozens of food trucks, live music, and games to half and M Street SE (across from Navy Yard Metro Station) today from 11:00 a.m. to 11:00 p.m.</p>'
},
geometry: {
type: 'Point',
coordinates: [-77.007481, 38.876516]
}
}
]
}
});
mapRef.current.addLayer({
id: 'places',
type: 'circle',
source: 'places',
paint: {
'circle-color': '#4264fb',
'circle-radius': 6,
'circle-stroke-width': 2,
'circle-stroke-color': '#ffffff'
}
});
const popup = new mapboxgl.Popup({
closeButton: false,
closeOnClick: false
});
mapRef.current.addInteraction('places-mouseenter-interaction', {
type: 'mouseenter',
target: { layerId: 'places' },
handler: (e) => {
mapRef.current.getCanvas().style.cursor = 'pointer';
// Copy the coordinates from the POI underneath the cursor
const coordinates = e.feature.geometry.coordinates.slice();
const description = e.feature.properties.description;
// Populate the popup and set its coordinates based on the feature found.
popup
.setLngLat(coordinates)
.setHTML(description)
.addTo(mapRef.current);
}
});
mapRef.current.addInteraction('places-mouseleave-interaction', {
type: 'mouseleave',
target: { layerId: 'places' },
handler: () => {
mapRef.current.getCanvas().style.cursor = '';
popup.remove();
}
});
});
}, []);
return (
<>
<div id="map" ref={mapContainerRef} style={{ height: '100%' }}></div>
</>
);
};
export default MapboxExample;
This code snippet will not work as expected until you replace
YOUR_MAPBOX_ACCESS_TOKEN
with an access token from your Mapbox account.Was this example helpful?