7

I'm trying to add multiple layers from a single Geoserver source to my Openlayers map. I can get each layer to appear by changing a single line of code. but when I attempt to have both layers on at once, I get a blank map. How do I add both? One is a raster, the other is a vector.

I've attempted via having the additional layer in a new variable and using map.addlayer. This didn't break the map, but it still didn't load the additional layer. I tried by just adding the additional layer into the layer variable, broke the map. I also tried by adding it to the params. So it was

params:{'LAYERS': 'Test:TestImagery', 'Test:Vector'},

This broke the map. How do I make this work?

Code:

<script>
  var layers = [
    new ol.layer.Tile({
      source: new ol.source.OSM()
    }),

    new ol.layer.Image({
      extent: [-20000000, 1000000, -5000000, 9000000],
      source: new ol.source.ImageWMS({
        url: 'http://localhost/geoserver/Test/wms',
        params: {'LAYERS': 'Test:TestImagery'},
        ratio: 1,
        serverType: 'geoserver'
      })
    })

  ];
  var map = new ol.Map({
    layers: layers,
    target: 'map',
    view: new ol.View({
      center: [0,0],
      zoom: 14
    })
  });

</script>
5
  • What was the error message when you requested both layers?
    – Ian Turton
    Commented Aug 10, 2017 at 22:56
  • I didn't get an error message the page just didn't load. Unless the error message appears elsewhere, in which case, where do I look?
    – user95191
    Commented Aug 10, 2017 at 22:58
  • The error message is in the response sent to you by geoserver
    – Ian Turton
    Commented Aug 10, 2017 at 22:59
  • 3
    What if you use : params:{'LAYERS': 'Test:TestImagery,Test:Vector'}, Also check what actual http request is made
    – nmtoken
    Commented Aug 11, 2017 at 7:44
  • @nmtoken This change worked.
    – user95191
    Commented Aug 11, 2017 at 15:42

1 Answer 1

5

Credit to nmtoken.

for having multiple layers from the same WMS, code should read:

params:{'LAYERS': 'Test:TestImagery,Test:Vector'},

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.