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?

3 Answers 3

  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.

  • I think this is a possible solution. Strangely, I am not facing this problem anymore. And I can't remember what I did. Maybe running the setBaseLayer immediately after initialization does the trick. Anyway thanks. Commented Nov 17, 2013 at 9:03

Set them all as not visible with base layer as true, then just make visible the one you want.


I usually use the isBaseLayer property to set the baseLayer, like this:

    var ol_wms = new OpenLayers.Layer.WMS.Untiled( "OpenLayers WMS", 
        "http://labs.metacarta.com/wms/vmap0?", {layers: 'basic'}, 
        { isBaseLayer: true} );
  var map = new OpenLayers.Map('map');
    map.setCenter(new OpenLayers.LonLat(0, 0), 0);

For more, see this article: BaseLayers and Overlays

  • No, it doesn't work. isBaseLayer only defines whether the layer is a base layer. We can have many different base layers, but only one can be set at any time. You can try add a few layers to understand what I mean. Commented Apr 13, 2013 at 12:24
  • If you have multiple Base Layers, then one which is top most (i.e added last) will be visible. So set whichever one you need last. Commented Apr 13, 2013 at 15:42
  • The first layer, layer[0] will be visible by default. But as I have stated in my question, I don't want to change the map order. Commented Apr 14, 2013 at 7:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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