Atlaslog
Atlaslog / Examples / OpenLayers / Arcgis Image
OpenLayers · Runnable Example

Arcgis Image

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

Source

demos/openlayers-arcgis-image.html
<!--
  示例:影像服务图层 — OpenLayers arcgis-image
  核心逻辑:Image 图层承载一张带图廓、图例、比例尺的服务出图,opacity 控制叠压效果
  依赖: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}
  .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:6px;align-items:center}
  #map{flex:1 1 auto;min-height:0;background:#eef0f3}
</style>
</head>
<body>
<div class="bar">
  <label>不透明度 <input type="range" id="op" min="0" max="100" value="85"></label>
  <label><input type="checkbox" id="tgl" checked> 服务图层</label>
  <span id="info">出图范围 116.28–116.50°E / 39.84–39.98°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 (typeof ol === 'undefined') {
    document.getElementById('map').innerHTML =
      '<div style="padding:20px;color:#b42318">OpenLayers 加载失败,请检查能否访问 cdn.jsdelivr.net</div>';
    return;
  }

  /* 模拟一次“导出地图”请求:图廓、分级设色、图例、比例尺都画在影像里 */
  function makeServiceImage() {
    var w = 1024, h = 768;
    var c = document.createElement('canvas');
    c.width = w;
    c.height = h;
    var ctx = c.getContext('2d');

    ctx.fillStyle = '#f7f5ee';
    ctx.fillRect(0, 0, w, h);

    /* 分级设色格网 */
    var cols = 16, rows = 12;
    for (var i = 0; i < cols; i++) {
      for (var j = 0; j < rows; j++) {
        var v = Math.abs(Math.sin(i / 3.2) * Math.cos(j / 2.6));
        var r = Math.round(235 - v * 140);
        var g = Math.round(240 - v * 90);
        var b = Math.round(225 - v * 60);
        ctx.fillStyle = 'rgb(' + r + ',' + g + ',' + b + ')';
        ctx.fillRect(60 + i * (w - 120) / cols, 60 + j * (h - 160) / rows,
          (w - 120) / cols + 1, (h - 160) / rows + 1);
      }
    }

    /* 河流 */
    ctx.strokeStyle = 'rgba(60,120,170,0.85)';
    ctx.lineWidth = 4;
    ctx.beginPath();
    ctx.moveTo(70, h * 0.62);
    ctx.bezierCurveTo(w * 0.35, h * 0.45, w * 0.55, h * 0.78, w - 70, h * 0.5);
    ctx.stroke();

    /* 图廓 */
    ctx.strokeStyle = '#2b3140';
    ctx.lineWidth = 2;
    ctx.strokeRect(50, 50, w - 100, h - 100);

    /* 图例 */
    ctx.fillStyle = 'rgba(255,255,255,0.94)';
    ctx.fillRect(70, h - 130, 230, 90);
    ctx.strokeStyle = '#7d8590';
    ctx.strokeRect(70, h - 130, 230, 90);
    ctx.fillStyle = '#2b3140';
    ctx.font = '13px sans-serif';
    ctx.fillText('指标等级', 84, h - 108);
    var levels = ['#ebecf0', '#c3d2c0', '#8fb08a', '#5b8a55'];
    for (var k = 0; k < 4; k++) {
      ctx.fillStyle = levels[k];
      ctx.fillRect(84 + k * 46, h - 96, 44, 16);
    }
    ctx.fillStyle = '#58606e';
    ctx.fillText('低', 84, h - 62);
    ctx.fillText('高', 246, h - 62);

    /* 比例尺 */
    ctx.strokeStyle = '#2b3140';
    ctx.lineWidth = 2;
    ctx.beginPath();
    ctx.moveTo(w - 260, h - 78);
    ctx.lineTo(w - 90, h - 78);
    ctx.stroke();
    ctx.beginPath();
    ctx.moveTo(w - 260, h - 84);
    ctx.lineTo(w - 260, h - 72);
    ctx.moveTo(w - 90, h - 84);
    ctx.lineTo(w - 90, h - 72);
    ctx.stroke();
    ctx.fillStyle = '#2b3140';
    ctx.fillText('0 — 5 km', w - 240, h - 92);

    return c.toDataURL();
  }

  var imageExtent = ol.proj.transformExtent(
    [116.28, 39.84, 116.50, 39.98], 'EPSG:4326', 'EPSG:3857'
  );

  var serviceSource = new ol.source.ImageStatic({
    url: makeServiceImage(),
    projection: 'EPSG:3857',
    imageExtent: imageExtent,
    crossOrigin: 'anonymous',
    interpolate: true
  });

  var serviceLayer = new ol.layer.Image({
    source: serviceSource,
    opacity: 0.85
  });

  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'}) }),
      serviceLayer
    ],
    view: new ol.View({
      center: ol.proj.fromLonLat([116.39, 39.91]),
      zoom: 12,
      minZoom: 2
    }),
    controls: ol.control.defaults.defaults({ attribution: false })
  });

  document.getElementById('op').addEventListener('input', function () {
    serviceLayer.setOpacity(this.value / 100);
  });

  document.getElementById('tgl').addEventListener('change', function () {
    serviceLayer.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 · EXAMPLEArcgis Tiled OpenLayers · EXAMPLEArea Measure OpenLayers · EXAMPLEAttribution Note