Static Image
Runnable OpenLayers example: Static Image. Open the live demo, read the complete source, and copy the file into your project. No build step or API key.
single HTML file · CDN dependencies onlyinterface may contain Chinese labels
Source
<!--
示例:静态图片叠加
要点:ImageStatic 把一张图片按 imageExtent 铺到指定地理范围
依赖: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>静态图片叠加</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="bnd" checked> 显示图片范围</label>
<span>范围 <b id="ext">—</b></span>
<span>图片按范围拉伸,投影为 EPSG:3857</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;
}
/* 用 canvas 画一张示意图,替代磁盘上的图片文件 */
function makeImage() {
var c = document.createElement('canvas');
c.width = 300;
c.height = 200;
var ctx = c.getContext('2d');
ctx.fillStyle = 'rgba(32,96,148,.82)';
ctx.fillRect(0, 0, 300, 200);
for (var i = 0; i < 7; i++) {
ctx.strokeStyle = 'rgba(255,255,255,' + (0.25 + i * 0.06) + ')';
ctx.lineWidth = 1.5;
ctx.beginPath();
ctx.arc(60 + i * 28, 100, 18 + i * 9, 0, Math.PI * 2);
ctx.stroke();
}
ctx.fillStyle = 'rgba(255,255,255,.9)';
ctx.font = '600 16px sans-serif';
ctx.fillText('IMAGE_STATIC', 16, 32);
return c.toDataURL();
}
/* 图片覆盖的西南角与东北角 */
var ll0 = [116.28, 39.84];
var ll1 = [116.52, 40.00];
var sw = ol.proj.fromLonLat(ll0);
var ne = ol.proj.fromLonLat(ll1);
var extent = [sw[0], sw[1], ne[0], ne[1]];
var imgLayer = new ol.layer.Image({
opacity: 0.75,
source: new ol.source.ImageStatic({
url: makeImage(),
imageExtent: extent,
projection: 'EPSG:3857'
})
});
var outlineLayer = new ol.layer.Vector({
source: new ol.source.Vector({
features: [new ol.Feature(new ol.geom.Polygon([[
[extent[0], extent[1]], [extent[2], extent[1]],
[extent[2], extent[3]], [extent[0], extent[3]],
[extent[0], extent[1]]
]]))]
}),
style: new ol.style.Style({
stroke: new ol.style.Stroke({ color: '#d64545', width: 2, lineDash: [7, 5] })
})
});
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'}) }),
imgLayer,
outlineLayer
],
view: new ol.View({ center: ol.proj.fromLonLat([116.40, 39.92]), zoom: 11 }),
controls: ol.control.defaults.defaults({ attribution: false, rotate: false })
});
document.getElementById('ext').textContent =
ll0[0] + ',' + ll0[1] + ' → ' + ll1[0] + ',' + ll1[1];
document.getElementById('bnd').addEventListener('change', function () {
outlineLayer.setVisible(this.checked);
});
map.getView().fit(extent, { padding: [60, 60, 60, 60], maxZoom: 13 });
})();
</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.