question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is it possible to combine <Layer paint> and click on <Feature> ?

See original GitHub issue

Hello !

I have this source

<Source id={'source-cities'} geoJsonSource={this.props.citiesGeoJSON} />

that I’m using for this layer

<Layer
  type='circle'
  sourceId={'source-cities'}
  id={'layer-1'}
  paint={
    'circle-radius': {
      property: 'population',
      type: 'exponential',
      stops: [
        // https://www.mapbox.com/mapbox-gl-js/style-spec#types-function-zoom-property
        [{zoom: 5, value: 0}, 3], // zoom, pop, radius
        [{zoom: 5, value: 855393}, 30], // zoom, pop, radius
    
        [{zoom: 10, value: 0}, 10], // zoom, pop, radius
        [{zoom: 10, value: 855393}, 50] // zoom, pop, radius
      ]
    }
  }
/>

And this is working fine, my circles radius are connected to the property ‘population’ .

But I can’t click on a point to display a popup. I need Feature component. So be it, I add all my Features :

<Layer
  type='circle'
  id={'clicklayer'}
  sourceId={'source-cities'}
  paint={
    'circle-radius': {
      property: 'population',
      type: 'exponential',
      stops: [
        // https://www.mapbox.com/mapbox-gl-js/style-spec#types-function-zoom-property
        [{zoom: 5, value: 0}, 3], // zoom, pop, radius
        [{zoom: 5, value: 855393}, 30], // zoom, pop, radius
    
        [{zoom: 10, value: 0}, 10], // zoom, pop, radius
        [{zoom: 10, value: 855393}, 50] // zoom, pop, radius
      ]
    }
  }
>
  {
    this.props.cities.map((city, i) => (
        <Feature
          key={i}
          onClick={() => console.log('click)}
          coordinates={city.geo}
        />
      ))
  }
</Layer>

Click on Feature is not working unless I remove the sourceId in Layer 😦 But if I do remove sourceId I am losing the paint (circle-radius depending on the property ‘population’.

How can I keep both click on Feature and paint style depending on a property ?

Thanks for your help !

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
daniel-hausercommented, Jan 8, 2019

We just released #675 that adds mouse events to <Layer> enjoy!

0reactions
vfoniccommented, Mar 26, 2018

Here’s another solution:

class PlacesMap extends React.Component {
  togglePopup = (map, { point, lngLat: { lng, lat } }) => {
    const [feature] = map.queryRenderedFeatures(point, { layers: ['fill'] });
    this.setState({ feature, popupLocation: [lng, lat] });
  }

  render() {
    return (
      <Mapbox
        {...props}
        onClick={this.togglePopup}
      >
        <Source id="places" geoJsonSource={this.geojson} />
        <Layer
          id="fill"
          type="fill"
          sourceId="places"
          paint={paint.fill}
        />
        {this.state.feature && (
          <Popup anchor="bottom" coordinates={this.state.popupLocation}>
            {this.popupContent}
          </Popup>
        )}
      </Mapbox>
    );
  }
}

I extracted this from real code. I hope it’s still usable.

Read more comments on GitHub >

github_iconTop Results From Across the Web

PaintShop Pro : How To Merge Layers Tutorial | Graphicxtras
How-to merge layers feature in PaintShop Pro, merge layers to create all kinds of amazing designs in PSP tutorial / Paintshop pro how-to ......
Read more >
Manage layers and groups in Photoshop - Adobe Support
Merge down: To merge two adjacent visible layers where the bottom layer is a pixel layer, select the top layer in the Layers...
Read more >
Draw a picture by combining and merging shapes
Select the shapes you want to merge: press and hold the Shift key while you select each shape in turn. (If you don't...
Read more >
How to rasterize, duplicate and merge layers - Drawing Desk
The Layer Merge feature combines the selected layer with the one directly below it in your panel of layers. Once two layers are...
Read more >
Introduction to Layers and Masks - Krita Manual
Usually, when you put one paint layer on top of another, the upper paint layer will be fully visible, while the layer behind...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found