1

I am looking for a nice plugin with working examples to group layers in OL 2.11/2.13.1 LayerSwitcher. I found several emails and posts explainig that GeoExt is the best way to do this, but have only access to OL.

3

There is a "unofficial" Control called OpenLayers.Control.LayerSwitcherGroups which you can find here:

https://raw.githubusercontent.com/rkj/openlayers/master/lib/OpenLayers/Control/LayerSwitcherGroups.js

Just include the Script after your OpenLayers.js and use it like this:

    var group = [hospitals, schools];
                for (var i=0; i < group.length; i++) { group[i].group = 'Hospitals + Schools'; }

    var group = [streets, police];
                for (var i=0; i < group.length; i++) { group[i].group = 'Streets + Police'; }


 map.addLayers([osm, streets, hospitals, schools, police]); 

Layerswitcher with groups Layerswitcher with groups folded / unfolded group

Apart from that there is a pretty old layerswitcher.js patch https://trac.osgeo.org/openlayers/ticket/1086 https://trac.osgeo.org/openlayers/attachment/ticket/1086/LayerSwitcher.js.patch Perhaps you could also use this and adapt it to the newer Layerswitcher.js version. The logic of this patch has been used for the Layer-Switcher-Control of HS-Layers where you can also find some more 'inspiration':

http://redmine.ccss.cz/projects/hslayers/repository/changes/trunk/source/HSLayers/Control/LayerSwitcher.js

| improve this answer | |
  • Thanks a lot Thomas, I tried it with OL2.11 and 2.13.1 but it doesn't work, what version have you tried? when you said Just include the Script after your OpenLayers.js you meant in the script tag or in the OL Control lib root? – Gery Jul 21 '15 at 15:43
  • 1
    sorry for the noise, I just needed to add map.addControl(new OpenLayers.Control.LayerSwitcherGroups());, so it works great, thanks!! – Gery Jul 21 '15 at 18:27
  • how to use open layer 2 layer switcher in Open Layers 3. – Seema Barate Aug 23 '17 at 15:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.