[GIS] How to set the base layer of map before initialization

layersopenlayers-2

I have been using map.setBaseLayer(layer); to switch to my preferred map layer, using the localStorage variable layer that is stored in my web browser, upon initialization of the map. But I realize that this is creating unnecessary calls to load the default (first layer) map tiles that I may not need if the preferred layer is the second or third one. Is there a way that I can pre-select the base layer to be displayed (before map initialization), without changing the order of the map layers?

Best Answer

  1. First You should define the layers but do not add them to map.
  2. Then set map.baseLayer = your-layer
  3. then add all layers to map.

OpenLayers add the first baselayer in map.baseLayer property but calls it only when add to map. So you can change the layer before being called.