如何设置开孔层绘制圆半径限制?



我想设置最大半径选项为ol.interaction.draw (type of Circle)。但是我找不到任何正式添加的选项。

我可以处理"drawend"事件,如果半径超过限制则手动取消。但这不是我想要的。如果达到半径限制,则绘图事件不应结束。画出来的圆应该停止扩张。用户可以通过绘制交互保持半径的变化,直到点击第二次(用户可以在同一时间内再次向下绘制)。

希望我把问题解释清楚了。

openlayer v3

您可以使用一个几何函数来生成一个具有最大半径的圆

<!doctype html>
<html lang="en">
<head>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.7.0/css/ol.css" type="text/css">
<style>
html, body, .map {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
}
#map {
position: relative;
}
#form {
z-index: 1;
opacity: 1;
position: absolute;
bottom: 0;
left: 0;
margin: 0;
}
</style>
<script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.7.0/build/ol.js"></script>
</head>
<body>
<div id="map" class="map"></div>
<form id="form">
<label>
Max radius
<input id="slider" type="range" min="1" max="200" value="100" />
+<span id="output"></span> meters
</label>
</form>
<script type="text/javascript">
const raster = new ol.layer.Tile({
source: new ol.source.OSM()
});
const source = new ol.source.Vector({ wrapX: false });
const vector = new ol.layer.Vector({
source: source
});
const map = new ol.Map({
layers: [raster, vector],
target: "map",
view: new ol.View({
center: [-11000000, 4600000],
zoom: 4
})
});
const slider = document.getElementById("slider");
const output = document.getElementById("output");
const scale = 5000;
slider.addEventListener("input", function () {
output.innerText = slider.value * scale;
});
output.innerText = slider.value * scale;
const draw = new ol.interaction.Draw({
source: source,
type: 'Circle',
geometryFunction: function (coordinates, geometry) {
const center = coordinates[0];
const last = coordinates[coordinates.length - 1];
const dx = center[0] - last[0];
const dy = center[1] - last[1];
const maxRadius = slider.value * scale;
const radius = Math.min(Math.sqrt(dx * dx + dy * dy), maxRadius);
if (!geometry) {
geometry = new ol.geom.Circle(center, radius);
} else {
geometry.setCenterAndRadius(center, radius);
}
return geometry;
}
});
map.addInteraction(draw);
</script>
</body>
</html>

相关内容

  • 没有找到相关文章