[GIS] Overlay WMS layers in OpenLayers

openlayers-2wms

I have three to four layers (shapefile data) in GeoServer, and I have to have all of them to make a map of a city. However, so far I can only create a map with only one layer by writing

map = new OpenLayers.Map('map');
var build_wms = new OpenLayers.Layer.WMS( "OpenLayers WMS",
                "http://localhost:8090/geoserver/wms/wsNYCRoad", {layers: 'building'}

If I add more layers by writing

var population_wms=...
var road_wms =...
map.addLayers([build_wms,population_wms,road_wms]);

then I can choose only one of these layers (in layer switcher) to be displayed in the browser.

How could I solve this problem? Do I have to set one of the layer as the base map and add other layers by using var vector? Then how can I convert those layers to .json file?

Best Answer

By default a WMS layer is considered to be an opaque base layer in OpenLayers. This means you can only see one at a time, to change this set isBaselayer: false

So for example:

  var build_wms = new OpenLayers.Layer.WMS( "OpenLayers WMS",
            "http://localhost:8090/geoserver/wms/wsNYCRoad", {layers: 'building'}
            {isBaselayer: false, transparent: true});

However you will need at least one baselayer to be set or the map will not be initialised properly.

Actually that's not completely true but it's close enough for this question