Measure
Runnable OpenLayers example: Measure. 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.sphere.getLength 实时算球面长度,Overlay 跟随鼠标
依赖: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:12px;align-items:center;font-size:12px;color:#58606e;flex-wrap:wrap}
.bar button{font:inherit;font-size:12px;padding:4px 9px;border:1px solid #ccd1d9;
border-radius:4px;background:#fff;color:#16181d;cursor:pointer}
#map{flex:1 1 auto;min-height:0;background:#eef0f3}
.tipbox{background:#16181d;color:#fff;padding:3px 8px;border-radius:4px;
font-size:12px;white-space:nowrap}
.tipbox::after{content:"";position:absolute;left:50%;bottom:-5px;margin-left:-5px;
border:5px solid transparent;border-top-color:#16181d;border-bottom:0}
</style>
</head>
<body>
<div class="bar">
<span>单击落点,双击结束测量</span>
<button id="clear">清空</button>
<span id="total">总长度:—</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;
}
var fromLonLat = ol.proj.fromLonLat;
var measureSource = new ol.source.Vector();
var measureLayer = new ol.layer.Vector({
source: measureSource,
style: new ol.style.Style({
stroke: new ol.style.Stroke({ color: '#d0453a', width: 3 }),
fill: new ol.style.Fill({ color: 'rgba(208,69,58,0.12)' })
})
});
var tipEl = document.createElement('div');
tipEl.className = 'tipbox';
tipEl.textContent = '';
var tipOverlay = new ol.Overlay({
element: tipEl,
positioning: 'bottom-center',
offset: [0, -12],
stopEvent: false
});
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'}) }),
measureLayer
],
overlays: [tipOverlay],
view: new ol.View({
center: fromLonLat([116.400, 39.915]),
zoom: 12
})
});
var totalEl = document.getElementById('total');
function formatLength(line) {
var meters = ol.sphere.getLength(line);
return meters >= 1000
? (meters / 1000).toFixed(2) + ' 公里'
: meters.toFixed(0) + ' 米';
}
var draw = new ol.interaction.Draw({
source: measureSource,
type: 'LineString',
style: new ol.style.Style({
stroke: new ol.style.Stroke({ color: '#d0453a', width: 3, lineDash: [8, 5] })
})
});
map.addInteraction(draw);
/* 绘制过程中跟随鼠标显示累计长度 */
draw.on('drawstart', function (evt) {
var geom = evt.feature.getGeometry();
var moveListener = function (e) {
if (e.dragging) return;
var clone = geom.clone();
clone.appendCoordinate(e.coordinate);
tipEl.textContent = formatLength(clone);
tipOverlay.setPosition(e.coordinate);
};
map.on('pointermove', moveListener);
draw.once('drawend', function () {
map.un('pointermove', moveListener);
tipOverlay.setPosition(undefined);
});
draw.once('drawabort', function () {
map.un('pointermove', moveListener);
tipOverlay.setPosition(undefined);
});
});
draw.on('drawend', function (evt) {
totalEl.textContent = '总长度:' + formatLength(evt.feature.getGeometry());
});
document.getElementById('clear').addEventListener('click', function () {
measureSource.clear();
totalEl.textContent = '总长度:—';
});
})();
</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.