walkermatt / ol-layerswitcher

Layer control for OpenLayers
MIT License
387 stars 176 forks source link

Only hide base layers from the same group. #360

Open paul121 opened 4 years ago

paul121 commented 4 years ago

It would be nice to make the base layer functionality (only one layer visible) apply to individual layer groups. This way only one layer from the layer group could be shown, while still showing a different base layer from a base layer group.

I believe this is functionality requested in #315

The way I have implemented this it would be "breaking" in the sense that it changes the current functionality of this library. If this is a problem, is there maybe an alternative way we could specify Layer Groups to behave in this manner? eg: Specify the Layer Group type = 'base', and then limit this behavior to "base" layer groups?

A specific use case is Drone imagery layers, where we want to display multiple raster TileLayers, but only one at a time from the "Drone Imagery" group. Example: base_layer_groups

paul121 commented 4 years ago

After looking closer at how the fold and combine layer group settings work I decided to make this functionality a setting on the layer group, too. If a layer group has the exclusive option set to true, its base layers will be treated separately from others. I chose the "exclusive" term as that is what the leaflet-groupedlayercontrol library uses to declare a similar feature.

The last thing that is bugging me is that individual layers need to be added with type = 'base', when they aren't really base layers. Perhaps we could introduce a new `type = 'exclusive`` layer type?

I've also updated this PR on top of the most recent v3.7.0 changes. @walkermatt I'm curious what you think; I think this would be a useful feature. I'm happy to help write tests and make any other changes to get this in!

walkermatt commented 4 years ago

I'm in support of this. I'd be happy to move away from having to set type: 'base' on individual layers and instead simply have the group have exclusive set as you've done. Do you have an example that you could provide a link to?

paul121 commented 4 years ago

I added an exclusive layer to the basic usage example included with this commit: https://github.com/walkermatt/ol-layerswitcher/pull/360/commits/06750f50b7b8f980384de43262ccd81577666b1a

I'd be happy to move away from having to set type: 'base' on individual layers and instead simply have the group have exclusive set as you've done.

The limitation with this is that you couldn't have base layers spanning multiple groups, which is the current behavior and perhaps seen as a feature (in other words, layers across multiple groups that are all mutually "exclusive"). Dropping support for type: 'base' would be a bit of a breaking change as well, but understand why you might want to.

walkermatt commented 3 years ago

I'm wondering about adding a property to each layer in an exclusive group equivalent to the radio button name. In this WIP branch I've used groupName but I'm not entirely happy with the name...

I think this could allow us to eventually drop type: base while still supporting having exclusive layers spanning multiple groups.

Demo with two separate exclusive groups: http://raw.githack.com/walkermatt/ol-layerswitcher/exclusive/examples/layerswitcher.html

paul121 commented 3 years ago

@walkermatt that looks great!

Maybe exclusiveGroup instead of groupName? It's tricky.. breaking this down a bit.. one level is configuring the exclusivity on the group level, another is configuring it on the layer level. Another level is a boolean exclusive config, vs a string exclusiveGroup name, where the string doesn't necessarily reference a layer group, but instead is just a unique identifier for the exclusive set.

A simplification would be to allow this configuration on just the group. So that group.exclusiveGroup = 'base`` would make all layers in that group radios. Another group could specify the sameexclusiveGrouptobase`, thus creating two groups that are mutually exclusive.

By allowing an exclusiveGroup to be configured on individual layers, this allows only SOME layers within the same group to have "exclusive" behavior, while others could still be checkboxes. I guess as this is the current behavior, so that should continue to be supported. So in this case, if the exclusiveGroup option is set on a layer's group, then it should inherit that same group.

This could maybe allow type: base to be a backwards compatible/deprecated way of assigning exclusiveGroup = 'base' on individual layers. So that option would still be supported & maintain its current behavior going forward, but the type option could be dropped in a later major release?

cmalzer commented 3 years ago

Hi everyone, I have the same problem (a layer group where I would like to have radio buttons, plus a group of basemaps) and just wanted to ask whether there are currently any plans for adding this feature? It would be really useful! :)

koba-1 commented 2 years ago

Hi everyone... I have the same need to have layers that can be viewed in an exclusive way...

kage212 commented 2 years ago

Hello, will this issue ever be closed? I mean this issue has been opened by two years and showed a potential fix, but still is not on the master branch...