如何将WMS添加到OpenLayers 5.3.0? [英] How do I add WMS to OpenLayers 5.3.0?

查看:149
本文介绍了如何将WMS添加到OpenLayers 5.3.0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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>

推荐答案

您可以查看该URL的GetCapabilities

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

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

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

这篇关于如何将WMS添加到OpenLayers 5.3.0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆