Add a hover tooltip that follows the mouse pointer
This example shows how to use a Popup with custom event listeners to create a tooltip that follows the mouse pointer when hovering over a polygon feature.
It uses Map.addInteraction to detect mousemove events over the state-fills layer, updating the popup's position and content based on the feature being hovered. When the mouse leaves the feature, a Map.on('mouseleave') event listener removes the popup from the map.
To add a highlight effect to complement the tooltip, see the Create a hover effect example
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Add a hover tooltip that follows the mouse pointer</title>
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no">
<link href="https://api.mapbox.com/mapbox-gl-js/v3.17.0-beta.1/mapbox-gl.css" rel="stylesheet">
<script src="https://api.mapbox.com/mapbox-gl-js/v3.17.0-beta.1/mapbox-gl.js"></script>
<style>
body { margin: 0; padding: 0; }
#map { position: absolute; top: 0; bottom: 0; width: 100%; }
</style>
</head>
<body>
<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: [-100.486052, 37.830348],
zoom: 2
});
// Create a popup, but don't add it to the map yet
const popup = new mapboxgl.Popup({
closeButton: false,
closeOnClick: false,
offset: [0, -20]
});
map.on('style.load', () => {
map.addSource('states', {
'type': 'geojson',
'data': 'https://docs.mapbox.com/mapbox-gl-js/assets/us_states.geojson'
});
map.addLayer({
'id': 'state-fills',
'type': 'fill',
'source': 'states',
'layout': {},
'paint': {
'fill-color': '#627BC1',
'fill-opacity': 0.5
}
});
map.addLayer({
'id': 'state-borders',
'type': 'line',
'source': 'states',
'layout': {},
'paint': {
'line-color': '#627BC1',
'line-width': 2
}
});
// use addInteraction for quick access to the feature under the mouse
map.addInteraction('state-fills-mousemove', {
type: 'mousemove',
target: {
layerId: 'state-fills'
},
handler: (e) => {
// Position the popup at the cursor location and show it
popup
.setLngLat(e.lngLat)
.setHTML(
`<strong>${e.feature.properties.STATE_NAME}</strong>`
)
.addTo(map);
}
});
// Use a standard Mapbox GL JS event listener for mouseleave to hide the popup
map.on('mouseleave', 'state-fills', () => {
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,
// Choose from Mapbox's core styles, or make your own style with Mapbox Studio
style: 'mapbox://styles/mapbox/standard',
center: [-100.486052, 37.830348],
zoom: 2
});
// Create a popup, but don't add it to the map yet
const popup = new mapboxgl.Popup({
closeButton: false,
closeOnClick: false,
offset: [0, -20]
});
mapRef.current.on('style.load', () => {
mapRef.current.addSource('states', {
type: 'geojson',
data: 'https://docs.mapbox.com/mapbox-gl-js/assets/us_states.geojson'
});
// The feature-state dependent fill-opacity expression will render the hover effect
// when a feature's hover state is set to true.
mapRef.current.addLayer({
id: 'state-fills',
type: 'fill',
source: 'states',
layout: {},
paint: {
'fill-color': '#627BC1',
'fill-opacity': 0.5
}
});
mapRef.current.addLayer({
id: 'state-borders',
type: 'line',
source: 'states',
layout: {},
paint: {
'line-color': '#627BC1',
'line-width': 2
}
});
// use addInteraction for quick access to the feature under the mouse
mapRef.current.addInteraction('state-fills-mousemove', {
type: 'mousemove',
target: {
layerId: 'state-fills'
},
handler: (e) => {
// Position the popup at the cursor location and show it
popup
.setLngLat(e.lngLat)
.setHTML(`<strong>${e.feature.properties.STATE_NAME}</strong>`)
.addTo(mapRef.current);
}
});
// Use a standard Mapbox GL JS event listener for mouseleave to hide the popup
mapRef.current.on('mouseleave', 'state-fills', () => {
popup.remove();
});
});
}, []);
return <div id="map" ref={mapContainerRef} style={{ height: '100%' }} />;
};
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?