Atlaslog
Atlaslog / Examples / OpenLayers / WMS
OpenLayers · Runnable Example

WMS

Runnable OpenLayers example: WMS. Open the live demo, read the complete source, and copy the file into your project. No build step or API key.

Open standalone → Chinese notes All OpenLayers examples
/demos/openlayers-wms.htmlLIVE
single HTML file · CDN dependencies onlyinterface may contain Chinese labels

Source

demos/openlayers-wms.html
<!--
  示例:WMS 图层
  要点:ol.source.TileWMS 接入 MapServer 服务,专题层叠加并调节透明度
  依赖:OpenLayers 10.3.1 · jsDelivr CDN
-->
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta name="robots" content="noindex,follow">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>WMS 图层</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/ol@10.3.1/ol.css">
<style>
  html,body{height:100%;margin:0}
  body{display:flex;flex-direction:column;overflow:hidden;
       font:13px/1.6 -apple-system,"Segoe UI","PingFang SC",sans-serif}
  .bar{flex:0 0 auto;padding:8px 12px;background:#f6f7f9;border-bottom:1px solid #e3e6ea;
       display:flex;gap:16px;align-items:center;font-size:12px;color:#58606e;flex-wrap:wrap}
  .bar label{display:flex;gap:5px;align-items:center;cursor:pointer}
  .bar input[type=range]{width:120px}
  #map{flex:1 1 auto;min-height:0;background:#eef0f3}
</style>
</head>
<body>
<div class="bar">
  <label><input type="checkbox" id="tglBorder" checked> 国界叠加</label>
  <label>透明度 <input type="range" id="opacity" min="0" max="100" value="70"><b id="opacityVal">70%</b></label>
  <span id="status">正在请求 WMS 瓦片</span>
</div>
<div id="map"></div>
<script src="https://cdn.jsdelivr.net/npm/ol@10.3.1/dist/ol.js"></script>
<script>
(function () {
  'use strict';

  if (typeof ol === 'undefined') {
    document.getElementById('map').innerHTML =
      '<div style="padding:20px;color:#b42318">OpenLayers 加载失败,请检查能否访问 cdn.jsdelivr.net</div>';
    return;
  }

  var fromLonLat = ol.proj.fromLonLat;
  var wmsUrl = 'https://demo.mapserver.org/cgi-bin/wms';

  /* 底图走 WMS:LAYERS 为服务端图层名,MapServer 不支持 TILED 参数 */
  var baseSource = new ol.source.TileWMS({
    url: wmsUrl,
    params: {
      'LAYERS': 'bluemarble',
      'VERSION': '1.1.1',
      'FORMAT': 'image/png'
    },
    serverType: 'mapserver',
    crossOrigin: 'anonymous'
  });

  var baseLayer = new ol.layer.Tile({
    source: baseSource,
    transition: 0
  });

  /* 专题层:国界线,透明背景叠在底图之上 */
  var borderSource = new ol.source.TileWMS({
    url: wmsUrl,
    params: {
      'LAYERS': 'country_bounds',
      'VERSION': '1.1.1',
      'FORMAT': 'image/png',
      'TRANSPARENT': true
    },
    serverType: 'mapserver',
    crossOrigin: 'anonymous'
  });

  var borderLayer = new ol.layer.Tile({
    source: borderSource,
    opacity: 0.7,
    transition: 0
  });

  var map = new ol.Map({
    target: 'map',
    layers: [baseLayer, borderLayer],
    view: new ol.View({
      center: fromLonLat([105, 32]),
      zoom: 3,
      minZoom: 1,
      maxZoom: 7
    })
  });

  var statusEl = document.getElementById('status');
  var loaded = false;

  baseSource.on('tileloadend', function () {
    if (loaded) return;
    loaded = true;
    statusEl.textContent = 'WMS 瓦片已返回,底图图层:bluemarble';
  });

  baseSource.on('tileloaderror', function () {
    statusEl.textContent = 'WMS 请求失败,检查服务地址与网络';
  });

  document.getElementById('tglBorder').addEventListener('change', function () {
    borderLayer.setVisible(this.checked);
  });

  document.getElementById('opacity').addEventListener('input', function () {
    var value = Number(this.value);
    borderLayer.setOpacity(value / 100);
    document.getElementById('opacityVal').textContent = value + '%';
  });
})();
</script>
</body>
</html>
Language: the code and this page are in English; the full tutorial and API notes for this example are in Chinese. The demo itself is the authoritative version — it runs exactly as shown.

More OpenLayers examples

OpenLayers · EXAMPLEWMS Capabilities OpenLayers · EXAMPLEWMS Custom Proj OpenLayers · EXAMPLEWMS Custom Tilegrid 512x256