markusand / mapboxgl-legend

Add a legend to a mapbox-gl map by parsing layer layout & paint expressions.
ISC License
49 stars 13 forks source link

Multiple legends if the layer ids are similar #17

Closed chriszrc closed 1 year ago

chriszrc commented 1 year ago

If I have layers in my map like "layer","layer1","layer2", and pass in config options like this:

mapLegendRef.current = new LegendControl({
          collapsed: false,
          layers: {
             ["layer"]:"fill-color"
          },
          toggler: true,
});

Layers "layer1" and "layer2" will also be added because of the broad regex matching here:

https://github.com/markusand/mapboxgl-legend/blob/c770843e3d971c3aeab76bbdb8287003752a87b6/src/LegendControl.ts#L104

Also, passing in a regex like [^layer$]:"fill-color" will work at first, but then the layer config appears to get overridden in subsequent updates, so that's not working either.

markusand commented 1 year ago

I see the problem. I've been playing a bit with accepting string and RegExp directly in layers array, but not if it's an object. Would that be an acceptable solution?

What updates do override layer config? Source updates?

chriszrc commented 1 year ago

@markusand Sure, I can use the array format instead if that's where regex was supported. I dug into the source a bit, when the layers that are actively added to the map change, then the layer config gets overridden-

markusand commented 1 year ago

Let's see if this is enough. Otherwise reopen the issue.