2

I've got a map style set up in Mapbox Studio. This style contains a layer with markers. I want to be able to click on this layer in my React project using react-map-gl.

In any other situation i would do something like

map.on('click', 'needed-layer', () => {this.dostuff()});

This does not seem to work in ReactMapGL.

I made a reference to the map like so:

<ReactMapGL
   ref={this.mapRef}
   ...
 />

this.mapRef = React.createRef();
this.map = this.mapRef.current.getMap();

If I log this.map i get a Map object which is good, but I would think the following code would detect a click on the predefined layer:

this.map.on('click', 'needed-layer', () => {this.dostuff()});

but it doesn't. Any ideas on how to click on a marker layer predefined in Mapbox Studio?

Cas
  • 162
  • 1
  • 13

1 Answers1

0

There is an onClick event which comes under the default Map (former ReactMapGL / MapGL) component.

You will need to specify the interactiveLayerIds property to make it work on specific layers.

Please refer to the section of onClick event from the most recent react-map-gl documentation.

Ethan
  • 346
  • 1
  • 5