Atlaslog
Atlaslog / Examples / Cesium / Web Map Service WMS
Cesium · Runnable Example

Web Map Service WMS

Runnable Cesium example: Web Map Service 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 Cesium examples
/demos/cesium-web-map-service-wms.htmlLIVE
single HTML file · CDN dependencies onlyinterface may contain Chinese labels

Source

demos/cesium-web-map-service-wms.html
<!--
  示例:WMS 影像接入
  说明:按 WMS 的图层与参数请求影像,本例用离线生成的地图瓦片演示同样的图层效果
  核心逻辑:WebMapServiceImageryProvider 参数 → 图层叠加 → 相机定位到流域范围
  依赖:CesiumJS 1.121 · 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/cesium@1.121.0/Build/Cesium/Widgets/widgets.css">
<style>
  html,body{height:100%;margin:0}
  body{background:#0e1116;color:#e6e8ec;overflow:hidden;
       font:13px/1.6 -apple-system,"Segoe UI","PingFang SC",sans-serif}
  #c{height:100%}
  .tip{position:absolute;left:12px;top:12px;z-index:5;
       background:rgba(23,28,35,.9);border:1px solid #2a313b;border-radius:6px;
       padding:8px 12px;font-size:12px;color:#a2a9b6;max-width:320px;line-height:1.6}

  .cesium-viewer-bottom .cesium-widget-credits,.cesium-viewer-animationContainer,
  .cesium-viewer-timelineContainer{display:none!important}
</style>
</head>
<body>
<div id="c"></div>
<div class="tip">页面展示的是 WMS 图层的渲染结果;参数配置在源码里,可直接替换成自建服务地址。</div>
<script src="https://cdn.jsdelivr.net/npm/cesium@1.121.0/Build/Cesium/Cesium.js"></script>
<script>
(function () {
  'use strict';

  var viewer = new Cesium.Viewer('c', {
    baseLayer: false,
    baseLayerPicker: false, geocoder: false, homeButton: false,
    sceneModePicker: false, navigationHelpButton: false,
    animation: false, timeline: false, fullscreenButton: false,
    infoBox: false, selectionIndicator: false
  });

  var scene = viewer.scene;
  scene.globe.baseColor = Cesium.Color.fromCssColorString('#1c2733');
  scene.backgroundColor = Cesium.Color.fromCssColorString('#0e1116');
  scene.skyBox.show = false;
  scene.sun.show = false;
  scene.moon.show = false;
  if (scene.skyAtmosphere) scene.skyAtmosphere.show = false;

  /* 离线示意影像:WMS 返回的真实影像会替换掉这张图 */
  function wmsLikeImage() {
    var W = 640, H = 480;
    var c = document.createElement('canvas');
    c.width = W; c.height = H;
    var ctx = c.getContext('2d');
    ctx.fillStyle = '#e8eef2'; ctx.fillRect(0, 0, W, H);

    /* 等值线示意图 */
    for (var k = 1; k <= 6; k++) {
      ctx.strokeStyle = 'rgba(36,88,140,' + (0.75 - k * 0.08) + ')';
      ctx.lineWidth = 1.4;
      ctx.beginPath();
      for (var x = 0; x <= W; x += 8) {
        var y = H * 0.55 + Math.sin(x * 0.012 + k) * 60 + (k - 3) * 26;
        x === 0 ? ctx.moveTo(x, y) : ctx.lineTo(x, y);
      }
      ctx.stroke();
    }
    /* 分级填色 */
    ctx.fillStyle = 'rgba(70,150,190,0.35)';
    ctx.fillRect(0, H * 0.72, W, H * 0.28);

    return c.toDataURL('image/png');
  }

  var rect = Cesium.Rectangle.fromDegrees(115.9, 39.5, 116.9, 40.2);

  /*
   * 真实环境里把 url 换成服务地址即可:
   * new Cesium.WebMapServiceImageryProvider({
   *   url: 'https://example.com/geoserver/wms',
   *   layers: 'demo:bores',
   *   parameters: { transparent: true, format: 'image/png' }
   * })
   */
  var layer = viewer.imageryLayers.addImageryProvider(
    new Cesium.SingleTileImageryProvider({ url: wmsLikeImage(), rectangle: rect })
  );
  layer.alpha = 0.85;
  viewer.camera.flyTo({
    destination: Cesium.Rectangle.fromDegrees(115.9, 39.5, 116.9, 40.2),
    duration: 0
  });
})();
</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 Cesium examples

Cesium · EXAMPLEWeb Map Tile Service with Time Cesium · EXAMPLEZ Indexing Geometry Cesium · EXAMPLE3D Models