Modify Test
Runnable OpenLayers example: Modify Test. 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
<!--
示例:修改交互的实时反馈
要点:modifystart/modifyend 记录编辑过程,逐次统计几何变化
依赖: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;flex-wrap:wrap}
.bar button{padding:4px 10px;border:1px solid #d5dae2;background:#fff;border-radius:4px;cursor:pointer}
.bar b{color:#16181d;font-weight:600}
#log{font-family:ui-monospace,Menlo,Consolas,monospace;font-size:11px}
#map{flex:1 1 auto;min-height:0;background:#eef0f3}
</style>
</head>
<body>
<div class="bar">
<span>顶点 <b id="vn">—</b></span>
<span>长度 <b id="len">—</b></span>
<span>修改次数 <b id="cnt">0</b></span>
<span id="log">等待编辑</span>
<button id="reset">复位</button>
</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 base = [
[116.30, 39.90], [116.355, 39.945], [116.415, 39.918],
[116.470, 39.952], [116.520, 39.900]
].map(function (c) { return fromLonLat(c); });
var line = new ol.Feature({ geometry: new ol.geom.LineString(base.slice()) });
line.setStyle(new ol.style.Style({
stroke: new ol.style.Stroke({ color: '#1f5fd0', width: 3 }),
image: new ol.style.Circle({
radius: 5,
fill: new ol.style.Fill({ color: '#ffffff' }),
stroke: new ol.style.Stroke({ color: '#1f5fd0', width: 2 })
})
}));
var source = new ol.source.Vector({ features: [line] });
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'}) }), new ol.layer.Vector({ source: source })],
view: new ol.View({ center: fromLonLat([116.41, 39.92]), zoom: 12 }),
controls: ol.control.defaults.defaults({ rotate: false })
});
var modify = new ol.interaction.Modify({ source: source, pixelTolerance: 10 });
map.addInteraction(modify);
var count = 0;
var startCoords = null;
function stats() {
var g = line.getGeometry();
var coords = g.getCoordinates();
document.getElementById('vn').textContent = coords.length;
document.getElementById('len').textContent = (ol.sphere.getLength(g) / 1000).toFixed(4) + ' km';
return coords;
}
modify.on('modifystart', function () {
startCoords = line.getGeometry().getCoordinates().slice();
document.getElementById('log').textContent = '开始拖动…';
});
modify.on('modifyend', function () {
count++;
document.getElementById('cnt').textContent = count;
var coords = stats();
/* 对比拖动前后,找出真正移动的顶点索引 */
var moved = [];
coords.forEach(function (c, i) {
var s = startCoords[i];
if (!s || c[0] !== s[0] || c[1] !== s[1]) moved.push(i);
});
var lonlat = moved.length
? ol.proj.toLonLat(coords[moved[moved.length - 1]])
: null;
document.getElementById('log').textContent = lonlat
? '顶点 #' + moved.join(',#') + ' 移到 ' + lonlat[0].toFixed(5) + ', ' + lonlat[1].toFixed(5)
: '这次没有顶点位置变化';
});
document.getElementById('reset').addEventListener('click', function () {
line.getGeometry().setCoordinates(base.slice());
count = 0;
document.getElementById('cnt').textContent = '0';
document.getElementById('log').textContent = '已复位';
stats();
});
stats();
})();
</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.