如何将WMS添加到OpenLayers 5.3.0?

时间:2019-01-23 17:06:49

标签: openlayers wms

我正在尝试将WMS叠加层集成到默认的OpenLayers地图上。仅使用ol.source.OSM({})图层,地图就可以很好地呈现,但是当我将WMS图层添加到layers: []数组中时,它仅给我一个空白地图。我正在使用以下代码,但是它不起作用,我需要更改什么?

<!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 个答案:

答案 0 :(得分:3)

您可以查看该网址http://www.igeo.pt/WMS/Geologia/CGP1M?SERVICE=WMS&REQUEST=GetCapabilities的GetCapabilities 有17层名为1到20的图层(缺少3、13和18),并带有葡萄牙语描述。在极少数情况下,您希望所有设置都如下所示(请注意,层构造函数只是ol.layer.Image(Lon / Lat顺序),并且我将其设置为半透明,因此背景仍然可见)< / p>

        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
        })

在全屏地图上,通常最好在平铺区域中请求WMS,以避免在平移时连续请求整个区域

        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
            })