diff --git a/src/ol/interaction/draw.js b/src/ol/interaction/draw.js index 970e157e91..554ec6573a 100644 --- a/src/ol/interaction/draw.js +++ b/src/ol/interaction/draw.js @@ -152,7 +152,11 @@ ol.interaction.Draw = function(options) { geometryFunction = function(coordinates, opt_geometry) { var geometry = opt_geometry; if (geometry) { - geometry.setCoordinates(coordinates); + if (mode === ol.interaction.Draw.Mode.POLYGON) { + geometry.setCoordinates([coordinates[0].concat([coordinates[0][0]])]); + } else { + geometry.setCoordinates(coordinates); + } } else { geometry = new Constructor(coordinates); } @@ -617,12 +621,10 @@ ol.interaction.Draw.prototype.finishDrawing = function() { coordinates.pop(); this.geometryFunction_(coordinates, geometry); } else if (this.mode_ === ol.interaction.Draw.Mode.POLYGON) { - // When we finish drawing a polygon on the last point, - // the last coordinate is duplicated as for LineString - // we force the replacement by the first point + // remove the redundant last point in ring coordinates[0].pop(); - coordinates[0].push(coordinates[0][0]); this.geometryFunction_(coordinates, geometry); + coordinates = geometry.getCoordinates(); } // cast multi-part geometries diff --git a/test/spec/ol/interaction/draw.test.js b/test/spec/ol/interaction/draw.test.js index 603a59dde7..e51b06f548 100644 --- a/test/spec/ol/interaction/draw.test.js +++ b/test/spec/ol/interaction/draw.test.js @@ -494,21 +494,30 @@ describe('ol.interaction.Draw', function() { map.addInteraction(draw); }); + function isClosed(polygon) { + var first = polygon.getFirstCoordinate(); + var last = polygon.getLastCoordinate(); + expect(first).to.eql(last); + } + it('draws polygon with clicks, finishing on first point', function() { // first point simulateEvent('pointermove', 10, 20); simulateEvent('pointerdown', 10, 20); simulateEvent('pointerup', 10, 20); + isClosed(draw.sketchFeature_.getGeometry()); // second point simulateEvent('pointermove', 30, 20); simulateEvent('pointerdown', 30, 20); simulateEvent('pointerup', 30, 20); + isClosed(draw.sketchFeature_.getGeometry()); // third point simulateEvent('pointermove', 40, 10); simulateEvent('pointerdown', 40, 10); simulateEvent('pointerup', 40, 10); + isClosed(draw.sketchFeature_.getGeometry()); // finish on first point simulateEvent('pointermove', 10, 20);