[英]Remove last point from Draw Interaction in Ol3
我試圖刪除esc鍵上的多邊形繪制功能中的最后一點。
以下代碼不起作用。 繪制區域部分時似乎已將其刪除,但頂點仍在那里。
var geom :ol.geom.Polygon = null;
draw.on("drawstart",(event) => {
console.log(event);
var feature :ol.Feature= event.feature;
console.log(feature);
geom = feature.getGeometry<ol.geom.Polygon>();
});
$(document).keyup((e)=> {
if (e.keyCode === 27) {
var coords = geom.getCoordinates()[0];
if(coords.length>1)
geom.setCoordinates([coords.slice(0, coords.length - 2)]);
}
});
OpenLayers 3使用Draw.removeLastPoint()方法對此有了新的解決方案。 奇跡般有效!
draw = new ol.interaction.Draw({ source:source, type: /** @type (ol.geom.GeometryType) */ ('Polygon') }) draw.on('drawstart', function(evt){ $(document).on('keyup', function(event){ if(event.keyCode === 27){ draw.removeLastPoint(); } }); });
刪除最后一個線段后,需要在幾何元素上使用setCoordinates。
這是一個例子:
this.olDraw.on('drawstart',
function (evt) {
$(document).on('keyup', function (event) {
if (event.keyCode === 27) {
var feature = evt.feature;
var geom = feature.getGeometry();
if (geom.getType() === LINE_STRING) {
geom.setCoordinates(geom.getCoordinates().slice(0,geom.getCoordinates().length - 1));
}
}
});
}, this);
這段代碼是對類似問題的另一個答案的修改版本(我再也找不到)。 使用此代碼段,您可以刪除lineString的最后插入點。 我認為它也應適用於多邊形。 試試看。
var lineStringdraw = new ol.interaction.Draw({
features: features,
type: "LineString",
geometryFunction:geometryChange
});
function geometryChange(coordinates, geometry){
if (!geometry) {
console.info("what did you do?");
geometry = new ol.geom.LineString(null);
}
var coords = geometry.getCoordinates();
var diff = coordinates.length - coords.length;
if (diff > 1) {
coordinates.splice(coordinates.length - diff, diff - 1);
if (coordinates.length === 1){
lineStringdraw.finishDrawing();
var emptyFeature = vector2.getSource().getFeatures()[vector2.getSource().getFeatures().length-1];
vector2.getSource().removeFeature(emptyFeature);
}
}
geometry.setCoordinates(coordinates);
coordinates= geometry.getCoordinates();
return geometry;
}
var keydown = function(evt){
var charCode = (evt.which) ? evt.which : evt.keyCode;
if (charCode === 27 && drawing === true){ //esc key
var geom = drawing_feature.getGeometry();
geom.setCoordinates(geom.getCoordinates().slice(0, -1));
undo=true;
}
};
var drawing=false,drawing_feature=null;
lineStringdraw.on('drawstart', function(e1) {
drawing = true;
drawing_feature = e1.feature;
drawing_feature.setProperties({'FID': featureID})
var featureGeom = drawing_feature.getGeometry();
});
lineStringdraw.on('drawend', function(e2) {
drawing = false;
drawing_feature = null;
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.