Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Event binding of Layer #1997

Open
lsbFlying opened this issue Sep 21, 2022 · 4 comments
Open

Event binding of Layer #1997

lsbFlying opened this issue Sep 21, 2022 · 4 comments
Labels

Comments

@lsbFlying
Copy link

Target Use Case

<Layer
type="fill"
id="chinaLayerFill"
paint={{
"fill-opacity": 0.35,
"fill-color": "#107DB3",
}}
// Expected support
// onClick={(e) => { ... }}
// onXXX={(e) => {}}
/>

Proposal

Because there are general business requirements, you only want to process binding events on certain layers

@danielvaughn
Copy link

Thanks for filing this @lsbFlying, I agree that this should be a supported feature. Do you have a workaround at the moment?

I'm facing a scenario where I have dozens of layers that need to be seamlessly mounted/unmounted/remounted as the user interacts with my application. Routing these events through <MapGL /> is going to be extremely cumbersome. I tried setting a ref on the <Layer /> component itself, but the imperative methods were removed from the ref in v7.

@hussamkhatib
Copy link

any update on this one ?

@bartlomiejwendt
Copy link

bartlomiejwendt commented Oct 25, 2022

@lsbFlying, You can freely interact with your layers with the onClick method in react-map-gl.

The only trick here is that you have to use your Map's component onClick method combined with interactiveLayersIds as stated in docs here: https://visgl.github.io/react-map-gl/docs/api-reference/map#onclick

In your case you will have to pass the layer id to the map interactiveLayerIds prop:

import Map from 'react-map-gl'

<Map 
  interactiveLayerIds={['chinaLayerFill']}
  onClick={(event) => {
    const feature = event.features[0]
    // handle your onClick layer logic here
  }}
>
  <Layer
    type="fill"
    id="chinaLayerFill"
    paint={{
      "fill-opacity": 0.35,
      "fill-color": "#107DB3",
    }}
  />
</Map>

This is how I handle this in my app, and it works without any issues.

I agree this is going to be quite messy (as danielvaughn mentioned above) when handling multiple layers and binding different actions/behaviours to them through the Map's onClick method but this is the only way that I can think of right now.

@lswainemoore
Copy link

lswainemoore commented Feb 14, 2024

Hi, sorry to open an old thread here, but I'm not confident this actually works. @bartlomiejwendt would you be willing to share code where you used this? This pattern is used in the clusters example, but doesn't work, because features doesn't get populated, as reported here. As far as I can tell from checking out old commits, this example never worked. Thanks!

EDIT: nevermind, see: #2243 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants