Reusable Source
Runnable OpenLayers example: Reusable Source. 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
<!--
示例:复用同一个数据源
要点:一个 ol.source.Vector 可以同时挂到多个图层、多个地图上
依赖: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 button{font-size:12px;padding:3px 10px;border:1px solid #cfd5de;background:#fff;
border-radius:5px;cursor:pointer;color:#16181d}
.bar b{color:#16181d;font-weight:600}
.row{flex:1 1 auto;display:flex;min-height:0}
.pane{flex:1 1 50%;position:relative;min-width:0}
.pane + .pane{border-left:1px solid #dfe3e9}
.pane .tag{position:absolute;left:8px;top:8px;z-index:2;background:rgba(255,255,255,.9);
border:1px solid #dfe3e9;border-radius:4px;padding:2px 7px;font-size:11px;color:#58606e}
.map{position:absolute;inset:0;background:#eef0f3}
</style>
</head>
<body>
<div class="bar">
<button id="add">新增一个要素</button>
<button id="clear">清空</button>
<span>要素数 <b id="n">0</b></span>
<span>两幅地图读同一个源,增删同步</span>
</div>
<div class="row">
<div class="pane"><span class="tag">地图 A</span><div id="mapA" class="map"></div></div>
<div class="pane"><span class="tag">地图 B</span><div id="mapB" class="map"></div></div>
</div>
<script src="https://cdn.jsdelivr.net/npm/ol@10.3.1/dist/ol.js"></script>
<script>
(function () {
'use strict';
if (!window.ol) {
document.body.innerHTML = '<p style="padding:20px;color:#b42318">OpenLayers 加载失败,请检查 cdn.jsdelivr.net 是否可访问</p>';
return;
}
var shared = new ol.source.Vector();
var style = new ol.style.Style({
image: new ol.style.Circle({
radius: 6,
fill: new ol.style.Fill({ color: '#1f5fd0' }),
stroke: new ol.style.Stroke({ color: '#fff', width: 2 })
})
});
/* 同一个 source 分别装进两个图层,两个图层又进两幅地图 */
function makeMap(target) {
return new ol.Map({
target: target,
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'}) }),
new ol.layer.Vector({ source: shared, style: style })
],
view: new ol.View({ center: ol.proj.fromLonLat([116.391, 39.907]), zoom: 12 }),
controls: ol.control.defaults.defaults({ attribution: false, rotate: false })
});
}
makeMap('mapA');
makeMap('mapB');
function syncCount() {
document.getElementById('n').textContent = shared.getFeatures().length;
}
document.getElementById('add').addEventListener('click', function () {
var lon = 116.30 + Math.random() * 0.2;
var lat = 39.86 + Math.random() * 0.1;
shared.addFeature(new ol.Feature({
geometry: new ol.geom.Point(ol.proj.fromLonLat([lon, lat]))
}));
syncCount();
});
document.getElementById('clear').addEventListener('click', function () {
shared.clear();
syncCount();
});
/* 预置几个点,打开页面即可看到两幅图内容一致 */
for (var i = 0; i < 5; i++) {
shared.addFeature(new ol.Feature({
geometry: new ol.geom.Point(ol.proj.fromLonLat([
116.34 + i * 0.026, 39.88 + (i % 3) * 0.015
]))
}));
}
syncCount();
})();
</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.