0
votes

I am trying to integrate a WMS overlay onto my default OpenLayers map. Using just the ol.source.OSM({}) layer the map renders fine, but when I add the WMS layer to the layers: [] array it just gives me a blank map. I am using the following code but it isn't working, what do I need to change?

<!doctype html>
<html lang="en">
  <head>
    <link rel="stylesheet" href="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.3.0/css/ol.css" type="text/css">
    <style>
      .map {
        height: 100%;
        width: 100%;
      }
    </style>
    <script src="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.3.0/build/ol.js"></script>
    <title>OpenLayers example</title>
  </head>
  <body>
    <h2>WMS Map</h2>
    <div id="map" class="map"></div>
    <script type="text/javascript">
      var map = new ol.Map({
        target: 'map',
        layers: [
            new ol.layer.Tile(
                {
                    source: new ol.source.OSM({})
                }),
            new ol.layer.ImageWMS(
                {
                    source: new ol.source.ImageWMS(
                    {
                        url: 'http://www.igeo.pt/WMS/Geologia/CGP1M'
                    })
                })
        ],
        view: new ol.View(
            {
                center: ol.proj.fromLonLat([37.41, 8.82]),
                zoom: 4
            })
      });
    </script>
  </body>
</html>
1
You will need to specify the WMS parameters (an object specifying at least LAYERS) see the example openlayers.org/en/v4.6.5/examples/wms-image.htmlMike
I see. How can I find out what the layers are? All I have is the WMS link.Steffan
its ol.layer.Image for ImageWMS source.Bharti Mohane

1 Answers

3
votes

You can look at the GetCapabilities for that url http://www.igeo.pt/WMS/Geologia/CGP1M?SERVICE=WMS&REQUEST=GetCapabilities There are 17 layers named 1 to 20 (3, 13 and 18 are missing) with Portuguese descriptions. In the unlikely event of you wanting all the setup would be as below (note the layer constructor is simply ol.layer.Image, the Lon/Lat order, and I've made it semi-opaque so the background is still visible)

        new ol.layer.Tile(
            {
                source: new ol.source.OSM({})
            }),
        new ol.layer.Image(
            {
                source: new ol.source.ImageWMS(
                {
                    url: 'http://www.igeo.pt/WMS/Geologia/CGP1M',
                    params: { LAYERS: '1,2,4,5,6,7,8,9,10,11,12,14,15,16,17,19,20'}
                }),
                opacity: 0.5
            })
    ],
    view: new ol.View(
        {
            center: ol.proj.fromLonLat([-8.82, 37.41]),
            zoom: 4
        })

On a full screen map it is usually more efficient to request the WMS in tiled areas to avoid continually requesting the whole area when panning

        new ol.layer.Tile(
            {
                source: new ol.source.TileWMS(
                {
                    url: 'http://www.igeo.pt/WMS/Geologia/CGP1M',
                    params: { LAYERS: '1,2,4,5,6,7,8,9,10,11,12,14,15,16,17,19,20'}
                }),
                opacity: 0.5
            })