Это можно сделать с помощью параметра geometryFunction
во взаимодействии, чтобы использовать геометрию существующего объекта вместо запуска нового.Рендеринг линий эскиза не работает должным образом, поэтому их нужно будет обрабатывать с помощью функции стиля.Если вы хотите использовать как рисование, так и изменение взаимодействий, когда одно начинается / заканчивается, вам может потребоваться отключить / повторно включить другое.
var white = [255, 255, 255, 1];
var blue = [0, 153, 255, 1];
var width = 3;
var drawStyles = [
new ol.style.Style({
fill: new ol.style.Fill({
color: [255, 255, 255, 0.5]
})
}),
new ol.style.Style({
stroke: new ol.style.Stroke({
color: white,
width: width + 2
})
}),
new ol.style.Style({
stroke: new ol.style.Stroke({
color: blue,
width: width
})
})
];
var pointStyle = new ol.style.Style({
image: new ol.style.Circle({
radius: width * 2,
fill: new ol.style.Fill({
color: blue
}),
stroke: new ol.style.Stroke({
color: white,
width: width / 2
})
}),
zIndex: Infinity
});
var raster = new ol.layer.Tile({
source: new ol.source.OSM()
});
var source = new ol.source.Vector();
var vector = new ol.layer.Vector({
source: source,
style: new ol.style.Style({
fill: new ol.style.Fill({
color: 'rgba(255, 255, 255, 0.2)'
}),
stroke: new ol.style.Stroke({
color: '#ffcc33',
width: 2
}),
image: new ol.style.Circle({
radius: 7,
fill: new ol.style.Fill({
color: '#ffcc33'
})
})
})
});
var map = new ol.Map({
layers: [raster, vector],
target: 'map',
view: new ol.View({
center: [-11000000, 4600000],
zoom: 4
})
});
var draw = new ol.interaction.Draw({
source: source,
type: 'Polygon',
geometryFunction: function(coordinates, geometry) {
if (geometry) {
if (coordinates[0].length) {
// Add a closing coordinate to match the first
geometry.setCoordinates([coordinates[0].concat([coordinates[0][0]])]);
} else {
geometry.setCoordinates([]);
}
} else {
var existing = source.getFeatures()[0];
if (existing) {
source.removeFeature(existing);
geometry = existing.getGeometry();
coordinates[0] = geometry.getCoordinates()[0].slice(0,-2).concat([coordinates[0][0]]);
geometry.setCoordinates([coordinates[0].concat([coordinates[0][0]])]);
} else {
geometry = new ol.geom.Polygon(coordinates);
}
}
return geometry;
},
style: function(feature) {
if (feature.getGeometry().getType() == 'Polygon') {
var sketchLine = new ol.geom.LineString(feature.getGeometry().getCoordinates()[0].slice(0,-1));
drawStyles[1].setGeometry(sketchLine);
drawStyles[2].setGeometry(sketchLine);
return drawStyles;
} else {
return pointStyle;
}
}
});
map.addInteraction(draw);
html, body, .map {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
}
<link href="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.3.0/css/ol.css" rel="stylesheet" />
<script src="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.3.0/build/ol.js"></script>
<div id="map" class="map"></div>
Самый простой способ отменить отмену активного взаимодействия при рисовании - это вызвать removeLastPoint()
, когда нажата клавиша, например Esc.Он будет работать с кодом, который я использовал на реальной странице, но слушатель keydown не работает внутри исполняемого фрагмента stackoverflow
document.addEventListener('keydown', function(e) {
if (e.which == 27)
draw.removeLastPoint();
});