Atlaslog
Atlaslog / Examples / OpenLayers / Svg Layer
OpenLayers · Runnable Example

Svg Layer

Runnable OpenLayers example: Svg Layer. 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-svg-layer.htmlLIVE
single HTML file · CDN dependencies onlyinterface may contain Chinese labels

Source

demos/openlayers-svg-layer.html
<!--
  示例:SVG 图层
  要点:ImageStatic 接受 data:image/svg+xml,矢量图形随范围缩放不糊
  依赖:OpenLayers 10.3.1 · jsDelivr CDN(底图 OSM,无 Key)
-->
<!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>SVG 图层</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;color:#16181d}
  .bar{flex:0 0 auto;padding:8px 12px;background:#f6f7f9;border-bottom:1px solid #e3e6ea;
       display:flex;gap:14px;align-items:center;font-size:12px;color:#58606e;flex-wrap:wrap}
  .bar label{display:flex;gap:5px;align-items:center;cursor:pointer}
  .bar b{color:#16181d;font-weight:600}
  #map{flex:1 1 auto;min-height:0;background:#eef0f3}
</style>
</head>
<body>
<div class="bar">
  <label><input type="checkbox" id="svg" checked> SVG 图层</label>
  <span>不透明度 <b id="op">85</b>%</span>
  <input type="range" id="opR" min="0" max="100" value="85" style="width:130px">
  <span>放大后线条依然锐利</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 (!window.ol) {
    document.getElementById('map').textContent = 'OpenLayers 加载失败,请检查 cdn.jsdelivr.net 是否可访问';
    return;
  }

  /* 直接把 SVG 源码作为 data URL 交给 ImageStatic */
  var svg = [
    '<svg xmlns="http://www.w3.org/2000/svg" width="600" height="400" viewBox="0 0 600 400">',
    '<defs><pattern id="hatch" width="12" height="12" patternUnits="userSpaceOnUse">',
    '<path d="M0 12 L12 0" stroke="#ffffff" stroke-opacity=".35" stroke-width="1"/>',
    '</pattern></defs>',
    '<rect width="600" height="400" fill="#1f5fd0" fill-opacity=".18"/>',
    '<rect x="40" y="40" width="240" height="150" fill="#1f5fd0" fill-opacity=".45"/>',
    '<rect x="40" y="40" width="240" height="150" fill="url(#hatch)"/>',
    '<circle cx="420" cy="130" r="90" fill="#2f8f5b" fill-opacity=".45"/>',
    '<path d="M60 340 Q 200 220 340 340 T 570 320" fill="none" stroke="#d64545" stroke-width="6"/>',
    '<text x="60" y="70" font-family="sans-serif" font-size="34" fill="#16181d">SVG overlay</text>',
    '<text x="60" y="380" font-family="sans-serif" font-size="18" fill="#3b4250">600 × 400 viewBox</text>',
    '</svg>'
  ].join('');

  var url = 'data:image/svg+xml;charset=utf-8,' + encodeURIComponent(svg);

  var sw = ol.proj.fromLonLat([116.30, 39.86]);
  var ne = ol.proj.fromLonLat([116.52, 40.00]);

  var svgLayer = new ol.layer.Image({
    opacity: 0.85,
    source: new ol.source.ImageStatic({
      url: url,
      imageExtent: [sw[0], sw[1], ne[0], ne[1]],
      projection: 'EPSG:3857'
    })
  });

  var map = new ol.Map({
    target: 'map',
    layers: [new ol.layer.Tile({ source: new ol.source.XYZ({url: 'https://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer/tile/{z}/{y}/{x}', attributions: 'Tiles © Esri'}) }), svgLayer],
    view: new ol.View({ center: ol.proj.fromLonLat([116.41, 39.93]), zoom: 12 }),
    controls: ol.control.defaults.defaults({ attribution: false, rotate: false })
  });

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

  document.getElementById('opR').addEventListener('input', function () {
    document.getElementById('op').textContent = this.value;
    svgLayer.setOpacity(this.value / 100);
  });
})();
</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 · EXAMPLESynthetic Lines OpenLayers · EXAMPLESynthetic Points OpenLayers · EXAMPLETeleport