Geoportal Extension for OpenLayers
API Reference

Class: LayerSwitcher

ol.control.LayerSwitcher

OpenLayers Control to manage map layers : their order, visibility and opacity, and display their informations (title, description, legends, metadata...)

new LayerSwitcher(options)

Controls/LayerSwitcher/LayerSwitcher.js, line 133
Name Type Description
options Object

control options

Name Type Description
layers Array optional

list of layers to be configured. Each array element is an object, with following properties :

Name Type Description
layer ol.layer.Layer optional

ol.layer.Layer layer to be configured (that has been added to map)

config Object optional

custom configuration object for layer information (title, description, legends, metadata, quicklook url), with following properties :

Name Type Description
title String optional

layer alias, to be displayed in widget layer list. E.g. : "Cartes IGN"

description String optional

layer description, to be displayed on title hover, or in layer information panel.

quicklookUrl String optional

link to a quick look image for this layer.

legends Array optional

array of layer legends. Each array element is an object, with following properties :
- url (String, mandatory) : link to a legend
- minScaleDenominator (Number, optional) : min scale denominator for legend validity.

metadata Array optional

array of layer metadata. Each array element is an object, with property url (String, mandatory) : link to a metadata

options Object optional

ol.control.Control options (see ol.control.Control)

Name Type Default Description
id Number optional

Ability to add an identifier on the widget (advanced option)

collapsed Boolean true optional

Specify if widget has to be collapsed (true) or not (false) on map loading. Default is true.

panel Boolean false optional

Specify if widget has to have a panel header. Default is false.

counter Boolean false optional

Specify if widget has to have a counter. Default is false.

allowEdit Boolean true optional

Specify if widget has to have an edit button (available only for vector layers). Default is true.

allowGrayScale Boolean true optional

Specify if widget has to have an grayscale button (not available for vector layers). Default is true.

advancedTools Array optional

...

Name Type Description
label String optional

Specify the label name of the button

icon String optional

icon (optionnal)

cb function optional

callback (optionnal)

styles Object optional

styles (optionnal)

Fires:
  • layerswitcher:add
  • layerswitcher:remove
  • layerswitcher:lock
  • layerswitcher:extent
  • layerswitcher:edit
  • layerswitcher:change:opacity
  • layerswitcher:change:visibility
  • layerswitcher:change:position
  • layerswitcher:change:grayscale
  • layerswitcher:change:style
  • layerswitcher:change:locked
Example
map.addControl(new ol.control.LayerSwitcher(
 [
     {
         layer : wms1,
         config : {
             title : "test layer name 1",
             description : "test layer desc 1",
         }
     }
 ],
 {
     collapsed : true,
     panel : false,
     counter : false,
     position : "top-left",
     allowEdit : true,
     allowGrayScale : true,
     advancedTools : [
         {
             label = 'Bouton',
             icon = "svg | http",
             cb = (e, LayerSwitcher, layer, options) => {},
             styles = {},
         }
     ]
 }
));

LayerSwitcher.on("layerswitcher:add", function (e) {
   console.warn("layer", e.layer);
});
LayerSwitcher.on("layerswitcher:remove", function (e) {
   console.warn("layer", e.layer);
});
LayerSwitcher.on("layerswitcher:extent", function (e) {
   console.warn("layer", e.layer);
});
LayerSwitcher.on("layerswitcher:edit", function (e) {
   console.warn("layer", e.layer);
});
LayerSwitcher.on("layerswitcher:change:opacity", function (e) {
   console.warn("layer", e.layer, e.opacity);
});
LayerSwitcher.on("layerswitcher:change:visibility", function (e) {
   console.warn("layer", e.layer, e.visibility);
});
LayerSwitcher.on("layerswitcher:change:position", function (e) {
   console.warn("layer", e.layer, e.position);
});
LayerSwitcher.on("layerswitcher:change:grayscale", function (e) {
   console.warn("layer", e.layer, e.grayscale);
});
LayerSwitcher.on("layerswitcher:change:style", function (e) {
   console.warn("layer", e.layer, e.name, e.url);
});
LayerSwitcher.on("layerswitcher:change:locked", function (e) {
   console.warn("layer", e.layer, e.locked);
});

Extends

  • ol.control.Control

Members

collapsed

collapse mode
true if widget is collapsed, false otherwise

Methods

addLayer(layer, config)

Controls/LayerSwitcher/LayerSwitcher.js, line 292

Add a new layer to control (when added to map) or add new layer configuration

Name Type Description
layer ol.layer.Layer

layer to add to layer switcher

config Object optional

additional options for layer configuration

Name Type Description
title Object optional

layer title (default is layer identifier)

description Object optional

layer description (default is null)

legends Object optional

layer legends (default is an empty array)

metadata Object optional

layer metadata (default is an empty array)

quicklookUrl Object optional

layer quicklookUrl (default is null)

Fires:
  • layerswitcher:add LayerSwitcher#event:ADD_LAYER_EVENT
Example
layerSwitcher.addLayer(
      gpParcels,
      {
          title : "Parcelles cadastrales",
          description : "description de la couche",
          quicklookUrl : "http://quicklookUrl.fr"
      }
  )

forget()

Controls/LayerSwitcher/LayerSwitcher.js, line 620

Forget add listener added to the control

getCollapsed(){Boolean}

Controls/LayerSwitcher/LayerSwitcher.js, line 577

Returns true if widget is collapsed (minimize), false otherwise

Returns:
collapsed

getContainer(){DOMElement}

Controls/LayerSwitcher/LayerSwitcher.js, line 613

Get container

getLayerInfo(layer){Object}

Controls/LayerSwitcher/LayerSwitcher.js, line 2237

Get layer informations : title, description, quicklookurl, legends, metadata

Name Type Description
layer Object

the ol.layer object

Returns:
- layer informations

isInRange(layer, map){Boolean}

Controls/LayerSwitcher/LayerSwitcher.js, line 2198

Check if map view is out of layer range (in terms of extent and zoom)

Name Type Description
layer Object

the ol.layer object

map Object

the ol.Map object

Returns:
- false if map view is out of layer range

listen()

Controls/LayerSwitcher/LayerSwitcher.js, line 628

Add listeners to catch map layers addition

lockLayer(layer, locked)

Controls/LayerSwitcher/LayerSwitcher.js, line 523

Lock a layer, so it cannot be removed or modified from layerSwitcher

Name Type Description
layer ol.layer.Layer

layer to be locked

locked Boolean

true if locked

Fires:
  • layerswitcher:lock LayerSwitcher#event:LOCK_LAYER_EVENT

removeLayer(layer)

Controls/LayerSwitcher/LayerSwitcher.js, line 461

Remove a layer from control

Name Type Description
layer ol.layer.Layer

layer.

Deprecated
  • on the future version ...
    Fires:
    • layerswitcher:remove LayerSwitcher#event:REMOVE_LAYER_EVENT

    setCollapsed(collapsed)

    Controls/LayerSwitcher/LayerSwitcher.js, line 552

    Collapse or display control main container

    Name Type Description
    collapsed Boolean

    True to collapse control, False to display it

    setMap(map)

    Controls/LayerSwitcher/LayerSwitcher.js, line 190

    Overload setMap function, that enables to catch map events, such as movend events.

    Name Type Description
    map ol.Map

    Map.

    setRemovable(layer, removable)

    Controls/LayerSwitcher/LayerSwitcher.js, line 587

    Display or hide removeLayerPicto from layerSwitcher for this layer

    Name Type Description
    layer ol.layer.Layer

    ol.layer to be configured

    removable Boolean

    specify if layer can be remove from layerSwitcher (true) or not (false). Default is true