[GIS] OpenLayers programmatically select base layer

openlayers-2

I have several base layers added to a map in an OpenLayers based application. There is only one that can be displayed at a time, as they are all base layers.

When the user changes the selected base layer, I store the name of the selected layer as a HTML5 local storage item (an improved cookie). When re-opening the application, I want to programmatically set the same base layer be displayed.

It works fine with layer.visibility = true if isBaseLayer is false, but not in the case when isBaseLayer is set to true.

Is there anything else I could try for this?

Best Answer

A combination of map.getLayersByName(name) and map.setBaseLayer(layer) should do this for you.