DrawFeature.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /* Copyright (c) 2006-2011 by OpenLayers Contributors (see authors.txt for
  2. * full list of contributors). Published under the Clear BSD license.
  3. * See http://svn.openlayers.org/trunk/openlayers/license.txt for the
  4. * full text of the license. */
  5. /**
  6. * @requires OpenLayers/Control.js
  7. * @requires OpenLayers/Feature/Vector.js
  8. */
  9. /**
  10. * Class: OpenLayers.Control.DrawFeature
  11. * The DrawFeature control draws point, line or polygon features on a vector
  12. * layer when active.
  13. *
  14. * Inherits from:
  15. * - <OpenLayers.Control>
  16. */
  17. OpenLayers.Control.DrawFeature = OpenLayers.Class(OpenLayers.Control, {
  18. /**
  19. * Property: layer
  20. * {<OpenLayers.Layer.Vector>}
  21. */
  22. layer: null,
  23. /**
  24. * Property: callbacks
  25. * {Object} The functions that are sent to the handler for callback
  26. */
  27. callbacks: null,
  28. /**
  29. * Constant: EVENT_TYPES
  30. *
  31. * Supported event types:
  32. * featureadded - Triggered when a feature is added
  33. */
  34. EVENT_TYPES: ["featureadded"],
  35. /**
  36. * APIProperty: multi
  37. * {Boolean} Cast features to multi-part geometries before passing to the
  38. * layer. Default is false.
  39. */
  40. multi: false,
  41. /**
  42. * APIProperty: featureAdded
  43. * {Function} Called after each feature is added
  44. */
  45. featureAdded: function() {},
  46. /**
  47. * APIProperty: handlerOptions
  48. * {Object} Used to set non-default properties on the control's handler
  49. */
  50. handlerOptions: null,
  51. /**
  52. * Constructor: OpenLayers.Control.DrawFeature
  53. *
  54. * Parameters:
  55. * layer - {<OpenLayers.Layer.Vector>}
  56. * handler - {<OpenLayers.Handler>}
  57. * options - {Object}
  58. */
  59. initialize: function(layer, handler, options) {
  60. // concatenate events specific to vector with those from the base
  61. this.EVENT_TYPES =
  62. OpenLayers.Control.DrawFeature.prototype.EVENT_TYPES.concat(
  63. OpenLayers.Control.prototype.EVENT_TYPES
  64. );
  65. OpenLayers.Control.prototype.initialize.apply(this, [options]);
  66. this.callbacks = OpenLayers.Util.extend(
  67. {
  68. done: this.drawFeature,
  69. modify: function(vertex, feature) {
  70. this.layer.events.triggerEvent(
  71. "sketchmodified", {vertex: vertex, feature: feature}
  72. );
  73. },
  74. create: function(vertex, feature) {
  75. this.layer.events.triggerEvent(
  76. "sketchstarted", {vertex: vertex, feature: feature}
  77. );
  78. }
  79. },
  80. this.callbacks
  81. );
  82. this.layer = layer;
  83. this.handlerOptions = this.handlerOptions || {};
  84. if (!("multi" in this.handlerOptions)) {
  85. this.handlerOptions.multi = this.multi;
  86. }
  87. var sketchStyle = this.layer.styleMap && this.layer.styleMap.styles.temporary;
  88. if(sketchStyle) {
  89. this.handlerOptions.layerOptions = OpenLayers.Util.applyDefaults(
  90. this.handlerOptions.layerOptions,
  91. {styleMap: new OpenLayers.StyleMap({"default": sketchStyle})}
  92. );
  93. }
  94. this.handler = new handler(this, this.callbacks, this.handlerOptions);
  95. },
  96. /**
  97. * Method: drawFeature
  98. */
  99. drawFeature: function(geometry) {
  100. var feature = new OpenLayers.Feature.Vector(geometry);
  101. var proceed = this.layer.events.triggerEvent(
  102. "sketchcomplete", {feature: feature}
  103. );
  104. if(proceed !== false) {
  105. feature.state = OpenLayers.State.INSERT;
  106. this.layer.addFeatures([feature]);
  107. this.featureAdded(feature);
  108. this.events.triggerEvent("featureadded",{feature : feature});
  109. }
  110. },
  111. /**
  112. * APIMethod: insertXY
  113. * Insert a point in the current sketch given x & y coordinates.
  114. *
  115. * Parameters:
  116. * x - {Number} The x-coordinate of the point.
  117. * y - {Number} The y-coordinate of the point.
  118. */
  119. insertXY: function(x, y) {
  120. if (this.handler && this.handler.line) {
  121. this.handler.insertXY(x, y);
  122. }
  123. },
  124. /**
  125. * APIMethod: insertDeltaXY
  126. * Insert a point given offsets from the previously inserted point.
  127. *
  128. * Parameters:
  129. * dx - {Number} The x-coordinate offset of the point.
  130. * dy - {Number} The y-coordinate offset of the point.
  131. */
  132. insertDeltaXY: function(dx, dy) {
  133. if (this.handler && this.handler.line) {
  134. this.handler.insertDeltaXY(dx, dy);
  135. }
  136. },
  137. /**
  138. * APIMethod: insertDirectionLength
  139. * Insert a point in the current sketch given a direction and a length.
  140. *
  141. * Parameters:
  142. * direction - {Number} Degrees clockwise from the positive x-axis.
  143. * length - {Number} Distance from the previously drawn point.
  144. */
  145. insertDirectionLength: function(direction, length) {
  146. if (this.handler && this.handler.line) {
  147. this.handler.insertDirectionLength(direction, length);
  148. }
  149. },
  150. /**
  151. * APIMethod: insertDeflectionLength
  152. * Insert a point in the current sketch given a deflection and a length.
  153. * The deflection should be degrees clockwise from the previously
  154. * digitized segment.
  155. *
  156. * Parameters:
  157. * deflection - {Number} Degrees clockwise from the previous segment.
  158. * length - {Number} Distance from the previously drawn point.
  159. */
  160. insertDeflectionLength: function(deflection, length) {
  161. if (this.handler && this.handler.line) {
  162. this.handler.insertDeflectionLength(deflection, length);
  163. }
  164. },
  165. /**
  166. * APIMethod: undo
  167. * Remove the most recently added point in the current sketch geometry.
  168. *
  169. * Returns:
  170. * {Boolean} An edit was undone.
  171. */
  172. undo: function() {
  173. return this.handler.undo && this.handler.undo();
  174. },
  175. /**
  176. * APIMethod: redo
  177. * Reinsert the most recently removed point resulting from an <undo> call.
  178. * The undo stack is deleted whenever a point is added by other means.
  179. *
  180. * Returns:
  181. * {Boolean} An edit was redone.
  182. */
  183. redo: function() {
  184. return this.handler.redo && this.handler.redo();
  185. },
  186. /**
  187. * APIMethod: finishSketch
  188. * Finishes the sketch without including the currently drawn point.
  189. * This method can be called to terminate drawing programmatically
  190. * instead of waiting for the user to end the sketch.
  191. */
  192. finishSketch: function() {
  193. this.handler.finishGeometry();
  194. },
  195. /**
  196. * APIMethod: cancel
  197. * Cancel the current sketch. This removes the current sketch and keeps
  198. * the drawing control active.
  199. */
  200. cancel: function() {
  201. this.handler.cancel();
  202. },
  203. CLASS_NAME: "OpenLayers.Control.DrawFeature"
  204. });