Atlaslog
Atlaslog / Examples / OpenLayers / Image Vector Layer
OpenLayers · Runnable Example

Image Vector Layer

Runnable OpenLayers example: Image Vector 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-image-vector-layer.htmlLIVE
single HTML file · CDN dependencies onlyinterface may contain Chinese labels

Source

demos/openlayers-image-vector-layer.html
<!--
  示例:影像底图叠加矢量层
  要点:ol.source.ImageStatic 贴一张页面生成的图,矢量层按同一经纬度范围对齐
  依赖: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>影像底图叠加矢量层</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}
  .bar label{display:flex;gap:5px;align-items:center;cursor:pointer}
  #map{flex:1 1 auto;min-height:0;background:#eef0f3}
</style>
</head>
<body>
<div class="bar">
  <label><input type="checkbox" id="cbImg" checked> 影像底图</label>
  <label><input type="checkbox" id="cbVec" checked> 矢量标注</label>
  <span>影像范围 116.28–116.53°E / 39.83–39.97°N</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;
  }
  var fromLonLat = ol.proj.fromLonLat;

  var bbox = [116.28, 39.83, 116.53, 39.97];
  var extent = ol.proj.transformExtent(bbox, 'EPSG:4326', 'EPSG:3857');

  /* 页面内画一张“街区图”,作为 ImageStatic 的影像来源 */
  var imageURL = (function () {
    var S = 512;
    var c = document.createElement('canvas');
    c.width = c.height = S;
    var ctx = c.getContext('2d');

    ctx.fillStyle = '#f2eee6';
    ctx.fillRect(0, 0, S, S);

    /* 街区 */
    var rows = 8, cols = 10;
    for (var r = 0; r < rows; r++) {
      for (var k = 0; k < cols; k++) {
        if ((r + k) % 7 === 0) continue;
        ctx.fillStyle = (r * cols + k) % 3 === 0 ? '#e4dccd' : '#dcd3c2';
        ctx.fillRect(k * (S / cols) + 6, r * (S / rows) + 6, S / cols - 12, S / rows - 12);
      }
    }

    /* 河流 */
    ctx.strokeStyle = '#9dc8e3';
    ctx.lineWidth = 26;
    ctx.beginPath();
    ctx.moveTo(0, S * 0.78);
    ctx.bezierCurveTo(S * 0.3, S * 0.62, S * 0.55, S * 0.9, S, S * 0.66);
    ctx.stroke();

    /* 快速路 */
    ctx.strokeStyle = '#f6c344';
    ctx.lineWidth = 10;
    ctx.beginPath();
    ctx.moveTo(S * 0.18, 0);
    ctx.lineTo(S * 0.34, S);
    ctx.moveTo(0, S * 0.36);
    ctx.lineTo(S, S * 0.44);
    ctx.stroke();

    ctx.strokeStyle = '#ffffff';
    ctx.lineWidth = 2;
    ctx.strokeRect(0.5, 0.5, S - 1, S - 1);

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

  var imageLayer = new ol.layer.Image({
    source: new ol.source.ImageStatic({
      url: imageURL,
      imageExtent: extent,
      projection: 'EPSG:3857'
    })
  });

  var vecSource = new ol.source.Vector();

  [[116.33, 39.92, '西站'], [116.40, 39.905, '中心广场'], [116.48, 39.94, '东坝仓']]
    .forEach(function (p) {
      vecSource.addFeature(new ol.Feature({
        geometry: new ol.geom.Point(fromLonLat([p[0], p[1]])),
        name: p[2],
        style: new ol.style.Style({
          image: new ol.style.Circle({
            radius: 6,
            fill: new ol.style.Fill({ color: '#b42318' }),
            stroke: new ol.style.Stroke({ color: '#ffffff', width: 2 })
          }),
          text: new ol.style.Text({
            text: p[2],
            offsetY: -14,
            font: '500 12px sans-serif',
            fill: new ol.style.Fill({ color: '#16181d' }),
            stroke: new ol.style.Stroke({ color: '#ffffff', width: 3 })
          })
        })
      }));
    });

  vecSource.addFeature(new ol.Feature({
    geometry: new ol.geom.LineString([
      fromLonLat([116.33, 39.92]), fromLonLat([116.40, 39.905]), fromLonLat([116.48, 39.94])
    ]),
    style: new ol.style.Style({
      stroke: new ol.style.Stroke({ color: 'rgba(31,95,208,0.85)', width: 3, lineDash: [10, 6] })
    })
  }));

  var vectorLayer = new ol.layer.Vector({ source: vecSource });

  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'}) }), imageLayer, vectorLayer],
    view: new ol.View({ center: fromLonLat([116.405, 39.90]), zoom: 12 }),
    controls: ol.control.defaults.defaults({ rotate: false })
  });

  document.getElementById('cbImg').addEventListener('change', function () {
    imageLayer.setVisible(this.checked);
  });
  document.getElementById('cbVec').addEventListener('change', function () {
    vectorLayer.setVisible(this.checked);
  });
})();
</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 · EXAMPLEImmediate Geographic OpenLayers · EXAMPLEJsts OpenLayers · EXAMPLEKeyboard Pan