[76] | 1 | /* Copyright (c) 2006-2010 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 | |
---|
| 7 | /** |
---|
| 8 | * @requires OpenLayers/Handler/Path.js |
---|
| 9 | * @requires OpenLayers/Geometry/Polygon.js |
---|
| 10 | */ |
---|
| 11 | |
---|
| 12 | /** |
---|
| 13 | * Class: OpenLayers.Handler.Polygon |
---|
| 14 | * Handler to draw a polygon on the map. Polygon is displayed on mouse down, |
---|
| 15 | * moves on mouse move, and is finished on mouse up. |
---|
| 16 | * |
---|
| 17 | * Inherits from: |
---|
| 18 | * - <OpenLayers.Handler.Path> |
---|
| 19 | * - <OpenLayers.Handler> |
---|
| 20 | */ |
---|
| 21 | OpenLayers.Handler.Polygon = OpenLayers.Class(OpenLayers.Handler.Path, { |
---|
| 22 | |
---|
| 23 | /** |
---|
| 24 | * Parameter: polygon |
---|
| 25 | * {<OpenLayers.Feature.Vector>} |
---|
| 26 | */ |
---|
| 27 | polygon: null, |
---|
| 28 | |
---|
| 29 | /** |
---|
| 30 | * Constructor: OpenLayers.Handler.Polygon |
---|
| 31 | * Create a Polygon Handler. |
---|
| 32 | * |
---|
| 33 | * Parameters: |
---|
| 34 | * control - {<OpenLayers.Control>} The control that owns this handler |
---|
| 35 | * callbacks - {Object} An object with a properties whose values are |
---|
| 36 | * functions. Various callbacks described below. |
---|
| 37 | * options - {Object} An optional object with properties to be set on the |
---|
| 38 | * handler |
---|
| 39 | * |
---|
| 40 | * Named callbacks: |
---|
| 41 | * create - Called when a sketch is first created. Callback called with |
---|
| 42 | * the creation point geometry and sketch feature. |
---|
| 43 | * modify - Called with each move of a vertex with the vertex (point) |
---|
| 44 | * geometry and the sketch feature. |
---|
| 45 | * point - Called as each point is added. Receives the new point geometry. |
---|
| 46 | * done - Called when the point drawing is finished. The callback will |
---|
| 47 | * recieve a single argument, the polygon geometry. |
---|
| 48 | * cancel - Called when the handler is deactivated while drawing. The |
---|
| 49 | * cancel callback will receive a geometry. |
---|
| 50 | */ |
---|
| 51 | initialize: function(control, callbacks, options) { |
---|
| 52 | OpenLayers.Handler.Path.prototype.initialize.apply(this, arguments); |
---|
| 53 | }, |
---|
| 54 | |
---|
| 55 | /** |
---|
| 56 | * Method: createFeature |
---|
| 57 | * Add temporary geometries |
---|
| 58 | * |
---|
| 59 | * Parameters: |
---|
| 60 | * pixel - {<OpenLayers.Pixel>} The initial pixel location for the new |
---|
| 61 | * feature. |
---|
| 62 | */ |
---|
| 63 | createFeature: function(pixel) { |
---|
| 64 | var lonlat = this.control.map.getLonLatFromPixel(pixel); |
---|
| 65 | this.point = new OpenLayers.Feature.Vector( |
---|
| 66 | new OpenLayers.Geometry.Point(lonlat.lon, lonlat.lat) |
---|
| 67 | ); |
---|
| 68 | this.line = new OpenLayers.Feature.Vector( |
---|
| 69 | new OpenLayers.Geometry.LinearRing([this.point.geometry]) |
---|
| 70 | ); |
---|
| 71 | this.polygon = new OpenLayers.Feature.Vector( |
---|
| 72 | new OpenLayers.Geometry.Polygon([this.line.geometry]) |
---|
| 73 | ); |
---|
| 74 | this.callback("create", [this.point.geometry, this.getSketch()]); |
---|
| 75 | this.point.geometry.clearBounds(); |
---|
| 76 | this.layer.addFeatures([this.polygon, this.point], {silent: true}); |
---|
| 77 | }, |
---|
| 78 | |
---|
| 79 | /** |
---|
| 80 | * Method: destroyFeature |
---|
| 81 | * Destroy temporary geometries |
---|
| 82 | */ |
---|
| 83 | destroyFeature: function() { |
---|
| 84 | OpenLayers.Handler.Path.prototype.destroyFeature.apply(this); |
---|
| 85 | this.polygon = null; |
---|
| 86 | }, |
---|
| 87 | |
---|
| 88 | /** |
---|
| 89 | * Method: drawFeature |
---|
| 90 | * Render geometries on the temporary layer. |
---|
| 91 | */ |
---|
| 92 | drawFeature: function() { |
---|
| 93 | this.layer.drawFeature(this.polygon, this.style); |
---|
| 94 | this.layer.drawFeature(this.point, this.style); |
---|
| 95 | }, |
---|
| 96 | |
---|
| 97 | /** |
---|
| 98 | * Method: getSketch |
---|
| 99 | * Return the sketch feature. |
---|
| 100 | * |
---|
| 101 | * Returns: |
---|
| 102 | * {<OpenLayers.Feature.Vector>} |
---|
| 103 | */ |
---|
| 104 | getSketch: function() { |
---|
| 105 | return this.polygon; |
---|
| 106 | }, |
---|
| 107 | |
---|
| 108 | /** |
---|
| 109 | * Method: getGeometry |
---|
| 110 | * Return the sketch geometry. If <multi> is true, this will return |
---|
| 111 | * a multi-part geometry. |
---|
| 112 | * |
---|
| 113 | * Returns: |
---|
| 114 | * {<OpenLayers.Geometry.Polygon>} |
---|
| 115 | */ |
---|
| 116 | getGeometry: function() { |
---|
| 117 | var geometry = this.polygon && this.polygon.geometry; |
---|
| 118 | if(geometry && this.multi) { |
---|
| 119 | geometry = new OpenLayers.Geometry.MultiPolygon([geometry]); |
---|
| 120 | } |
---|
| 121 | return geometry; |
---|
| 122 | }, |
---|
| 123 | |
---|
| 124 | /** |
---|
| 125 | * Method: dblclick |
---|
| 126 | * Handle double-clicks. Finish the geometry and send it back |
---|
| 127 | * to the control. |
---|
| 128 | * |
---|
| 129 | * Parameters: |
---|
| 130 | * evt - {Event} |
---|
| 131 | */ |
---|
| 132 | dblclick: function(evt) { |
---|
| 133 | if(!this.freehandMode(evt)) { |
---|
| 134 | // remove the penultimate point |
---|
| 135 | var index = this.line.geometry.components.length - 2; |
---|
| 136 | this.line.geometry.removeComponent(this.line.geometry.components[index]); |
---|
| 137 | this.removePoint(); |
---|
| 138 | this.finalize(); |
---|
| 139 | } |
---|
| 140 | return false; |
---|
| 141 | }, |
---|
| 142 | |
---|
| 143 | CLASS_NAME: "OpenLayers.Handler.Polygon" |
---|
| 144 | }); |
---|