diff --git a/externs/olx.js b/externs/olx.js index f642f94bfe..3cf5845f20 100644 --- a/externs/olx.js +++ b/externs/olx.js @@ -1608,7 +1608,8 @@ olx.interaction.DragZoomOptions.prototype.style; * snapTolerance: (number|undefined), * type: ol.geom.GeometryType, * minPointsPerRing: (number|undefined), - * style: (ol.style.Style|Array.|ol.feature.StyleFunction|undefined)}} + * style: (ol.style.Style|Array.|ol.feature.StyleFunction|undefined), + * geometryName: (string|undefined)}} * @todo api */ olx.interaction.DrawOptions; @@ -1658,6 +1659,13 @@ olx.interaction.DrawOptions.prototype.minPointsPerRing; olx.interaction.DrawOptions.prototype.style; +/** + * Geometry name to use for features created by the draw interaction. + * @type {string|undefined} + */ +olx.interaction.DrawOptions.prototype.geometryName; + + /** * @typedef {{condition: (ol.events.ConditionType|undefined), * pixelDelta: (number|undefined)}} diff --git a/src/ol/interaction/drawinteraction.js b/src/ol/interaction/drawinteraction.js index 15d36386e5..722b99cae6 100644 --- a/src/ol/interaction/drawinteraction.js +++ b/src/ol/interaction/drawinteraction.js @@ -182,6 +182,13 @@ ol.interaction.Draw = function(options) { options.style : ol.interaction.Draw.getDefaultStyleFunction() }); + /** + * Name of the geometry attribute for newly created features. + * @type {string|undefined} + * @private + */ + this.geometryName_ = options.geometryName; + }; goog.inherits(ol.interaction.Draw, ol.interaction.Pointer); @@ -350,7 +357,11 @@ ol.interaction.Draw.prototype.startDrawing_ = function(event) { } } goog.asserts.assert(goog.isDef(geometry)); - this.sketchFeature_ = new ol.Feature(geometry); + this.sketchFeature_ = new ol.Feature(); + if (goog.isDef(this.geometryName_)) { + this.sketchFeature_.setGeometryName(this.geometryName_); + } + this.sketchFeature_.setGeometry(geometry); this.updateSketchFeatures_(); this.dispatchEvent(new ol.DrawEvent(ol.DrawEventType.DRAWSTART, this.sketchFeature_)); diff --git a/test/spec/ol/interaction/drawinteraction.test.js b/test/spec/ol/interaction/drawinteraction.test.js index db1f36e537..14fa5deaae 100644 --- a/test/spec/ol/interaction/drawinteraction.test.js +++ b/test/spec/ol/interaction/drawinteraction.test.js @@ -69,6 +69,28 @@ describe('ol.interaction.Draw', function() { }); + describe('specifying a geometryName', function() { + + beforeEach(function() { + var draw = new ol.interaction.Draw({ + source: source, + geometryName: 'the_geom', + type: ol.geom.GeometryType.POINT + }); + map.addInteraction(draw); + }); + + it('creates a feature with the correct geometryName', function() { + simulateEvent('pointermove', 10, 20); + simulateEvent('pointerdown', 10, 20); + simulateEvent('pointerup', 10, 20); + var features = source.getFeatures(); + var geometry = features[0].getGeometry(); + expect(features[0].getGeometryName()).to.equal('the_geom'); + expect(geometry).to.be.a(ol.geom.Point); + }); + }); + describe('drawing points', function() { var draw;