Picking, Clicks, Hover, and Tooltips

Handle layer clicks and hover with deck.gl PickingInfo and understand how these differ from Mapbox map events.

🎯 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 in data.
  • coordinate: the geographic coordinates corresponding to the pointer position.
  • x and y: 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:

Hover over a point for a tooltip, or click for details.
Click any point to see its underlying 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: true to support picking.
  • PickingInfo.object provides the underlying application data.
  • onHover suits tooltips; onClick suits 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

  1. In onClick, display the status from info.object in the details panel.
  2. Modify getFillColor so that the selected point uses a different color.

🎉 You can now connect map graphics to application interactions.