Tracing
Runnable OpenLayers example: Tracing. 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
<!--
示例:OpenLayers 边界追踪绘制
要点:Draw 的 trace 选项让绘制线沿现有面要素边界吸附前行
依赖: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:10px;align-items:center;flex-wrap:wrap;font-size:12px;color:#58606e}
.bar label{display:flex;gap:5px;align-items:center;cursor:pointer}
.bar select,.bar input[type=range]{font-size:12px}
.bar .val{font-family:ui-monospace,Consolas,monospace;color:#1f5fd0;min-width:44px}
.bar .sep{width:1px;height:16px;background:#d8dce2}
#map{flex:1 1 auto;min-height:0;background:#eef0f3}
</style>
</head>
<body>
<div class="bar">
<label>绘制类型
<select id="type">
<option value="Polygon">Polygon</option>
<option value="LineString">LineString</option>
<option value="None">关闭</option>
</select>
</label>
<button id="clear" type="button">清空</button>
<span class="sep"></span>
<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 (typeof ol === 'undefined') {
document.getElementById('map').textContent = 'OpenLayers 加载失败,请检查能否访问 cdn.jsdelivr.net';
return;
}
var center = ol.proj.fromLonLat([116.391, 39.907]);
var parcels = [];
for (var row = 0; row < 4; ++row) {
for (var col = 0; col < 4; ++col) {
var lon = 116.360 + col * 0.012;
var lat = 39.885 + row * 0.012;
var ring = [
ol.proj.fromLonLat([lon, lat]),
ol.proj.fromLonLat([lon + 0.010, lat]),
ol.proj.fromLonLat([lon + 0.010, lat + 0.010]),
ol.proj.fromLonLat([lon, lat + 0.010]),
ol.proj.fromLonLat([lon, lat])
];
parcels.push(new ol.Feature(new ol.geom.Polygon([ring])));
}
}
var baseSource = new ol.source.Vector({features: parcels});
var baseLayer = new ol.layer.Vector({
source: baseSource,
style: new ol.style.Style({
fill: new ol.style.Fill({color: 'rgba(220, 60, 60, 0.12)'}),
stroke: new ol.style.Stroke({color: 'rgba(200, 40, 40, 0.85)', width: 1})
})
});
var drawSource = new ol.source.Vector();
var drawLayer = new ol.layer.Vector({
source: drawSource,
style: new ol.style.Style({
fill: new ol.style.Fill({color: 'rgba(70, 200, 90, 0.22)'}),
stroke: new ol.style.Stroke({color: 'rgba(30, 150, 60, 0.95)', width: 2})
})
});
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'}), opacity: 0.75}),
baseLayer,
drawLayer
],
view: new ol.View({center: center, zoom: 16})
});
var snap = new ol.interaction.Snap({source: baseSource});
var drawInteraction = null;
function addInteraction() {
var value = document.getElementById('type').value;
if (drawInteraction) {
map.removeInteraction(drawInteraction);
drawInteraction = null;
}
if (value !== 'None') {
drawInteraction = new ol.interaction.Draw({
type: value,
source: drawSource,
trace: true,
traceSource: baseSource,
style: new ol.style.Style({
stroke: new ol.style.Stroke({color: 'rgba(255, 190, 0, 0.95)', width: 2}),
fill: new ol.style.Fill({color: 'rgba(255, 190, 0, 0.18)'}),
image: new ol.style.Circle({
radius: 5,
fill: new ol.style.Fill({color: 'rgba(255, 190, 0, 0.8)'})
})
})
});
map.addInteraction(drawInteraction);
}
}
document.getElementById('type').addEventListener('change', function () {
addInteraction();
});
document.getElementById('clear').addEventListener('click', function () {
drawSource.clear();
});
map.addInteraction(snap);
addInteraction();
})();
</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.