Picking, Clicks, Hover, and Tooltips
🎯 Introduction
By the end of this article, you will be able to make deck.gl graphics respond to clicks and hover, retrieve the underlying application object, and display the result in a tooltip or details panel.
🧱 Enable picking
By default, deck.gl only draws graphics. Setting pickable: true on a layer enables picking information so that onClick and onHover can identify the record under the pointer.
const layer = new ScatterplotLayer({
id: 'stations',
data: stations,
pickable: true,
getPosition: (station) => [station.lng, station.lat],
getRadius: 500,
getFillColor: [37, 99, 235, 220],
onClick: (info) => {
console.log(info.object);
},
onHover: (info) => {
console.log(info.object);
},
});
overlay.setProps({ layers: [layer] });
🔎 Understanding PickingInfo
PickingInfo is the information object deck.gl passes to event callbacks. Common fields include:
object: the underlying application record that was picked.index: the record's index indata.coordinate: the geographic coordinates corresponding to the pointer position.xandy: the screen pixel coordinates of the event.
object connects a graphic to its application details. Use it to update Vue state, filter a sidebar, or request details from an API.
💡 Tooltips and details panels
Use the overlay's getTooltip for temporary hover information, and update page state in onClick for persistent details:
const overlay = new MapboxOverlay({
interleaved: true,
getTooltip: ({ object }) => object && {
html: `<strong>${object.name}</strong><br>Count: ${object.count}`,
},
layers: [layer],
});
getTooltip works well for quick inspection, while a details panel can show more complete information. For an information window anchored to a map location, you can also pass PickingInfo.coordinate to a Mapbox Popup. This is a separate presentation option from deck.gl's tooltip.
The following example combines hover tooltips, click details, and the underlying application data:
⚡ Mapbox events and deck.gl events
The page contains two event systems:
deck.gl layer: onClick and onHover handle picking
Mapbox map: click, mousemove, move, and zoomend handle the map and camera
Use a layer's onClick for clicks on a deck.gl point, and map.on('zoomend') to detect when zooming ends. Keep map movement handling separate from graphic picking.
🧾 Summary
- A layer needs
pickable: trueto support picking. PickingInfo.objectprovides the underlying application data.onHoversuits tooltips;onClicksuits selection and details panels.- Mapbox events handle the map; deck.gl events handle graphics.
❓ Questions and answers
Q1: Why does onClick have no object?
First check that the layer has pickable: true, then confirm that the click actually lands on a graphic.
Q2: Should I use a Mapbox Popup or a deck.gl tooltip?
Use a deck.gl tooltip for hover inspection. A Mapbox Popup works well for a window anchored to a geographic location after a click.
Q3: How do coordinate and object.position differ?
object.position is a coordinate stored in the application data. coordinate is the map coordinate of this pointer event. They serve different purposes.
🧪 Exercises
- In
onClick, display thestatusfrominfo.objectin the details panel. - Modify
getFillColorso that the selected point uses a different color.
🎉 You can now connect map graphics to application interactions.
