diff --git a/src/ol/interaction/drawinteraction.js b/src/ol/interaction/drawinteraction.js index 283d738a43..3451b5749d 100644 --- a/src/ol/interaction/drawinteraction.js +++ b/src/ol/interaction/drawinteraction.js @@ -2,6 +2,7 @@ goog.provide('ol.DrawEvent'); goog.provide('ol.interaction.Draw'); goog.require('goog.asserts'); +goog.require('goog.events'); goog.require('goog.events.Event'); goog.require('ol.Collection'); goog.require('ol.Coordinate'); @@ -10,6 +11,7 @@ goog.require('ol.FeatureOverlay'); goog.require('ol.Map'); goog.require('ol.MapBrowserEvent'); goog.require('ol.MapBrowserEvent.EventType'); +goog.require('ol.Object'); goog.require('ol.events.condition'); goog.require('ol.geom.GeometryType'); goog.require('ol.geom.LineString'); @@ -18,6 +20,7 @@ goog.require('ol.geom.MultiPoint'); goog.require('ol.geom.MultiPolygon'); goog.require('ol.geom.Point'); goog.require('ol.geom.Polygon'); +goog.require('ol.interaction.InteractionProperty'); goog.require('ol.interaction.Pointer'); goog.require('ol.source.Vector'); goog.require('ol.style.Style'); @@ -204,6 +207,10 @@ ol.interaction.Draw = function(options) { this.condition_ = goog.isDef(options.condition) ? options.condition : ol.events.condition.noModifierKeys; + goog.events.listen(this, + ol.Object.getChangeEventType(ol.interaction.InteractionProperty.ACTIVE), + this.updateState_, false, this); + }; goog.inherits(ol.interaction.Draw, ol.interaction.Pointer); @@ -223,12 +230,8 @@ ol.interaction.Draw.getDefaultStyleFunction = function() { * @inheritDoc */ ol.interaction.Draw.prototype.setMap = function(map) { - if (goog.isNull(map)) { - // removing from a map, clean up - this.abortDrawing_(); - } - this.overlay_.setMap(map); goog.base(this, 'setMap', map); + this.updateState_(); }; @@ -562,6 +565,19 @@ ol.interaction.Draw.prototype.updateSketchFeatures_ = function() { }; +/** + * @private + */ +ol.interaction.Draw.prototype.updateState_ = function() { + var map = this.getMap(); + var active = this.getActive(); + if (goog.isNull(map) || !active) { + this.abortDrawing_(); + } + this.overlay_.setMap(active ? map : null); +}; + + /** * Get the drawing mode. The mode for mult-part geometries is the same as for * their single-part cousins.