1 /* Copyright (c) 2006-2008 MetaCarta, Inc., published under the Clear BSD
2 * license. See http://svn.openlayers.org/trunk/openlayers/license.txt for the
3 * full text of the license. */
7 * @requires OpenLayers/Handler/Path.js
8 * @requires OpenLayers/Geometry/Polygon.js
12 * Class: OpenLayers.Handler.Polygon
13 * Handler to draw a polygon on the map. Polygon is displayed on mouse down,
14 * moves on mouse move, and is finished on mouse up.
17 * - <OpenLayers.Handler.Path>
18 * - <OpenLayers.Handler>
20 OpenLayers.Handler.Polygon = OpenLayers.Class(OpenLayers.Handler.Path, {
24 * {<OpenLayers.Feature.Vector>}
29 * Constructor: OpenLayers.Handler.Polygon
30 * Create a Polygon Handler.
33 * control - {<OpenLayers.Control>} The control that owns this handler
34 * callbacks - {Object} An object with a properties whose values are
35 * functions. Various callbacks described below.
36 * options - {Object} An optional object with properties to be set on the
40 * create - Called when a sketch is first created. Callback called with
41 * the creation point geometry and sketch feature.
42 * modify - Called with each move of a vertex with the vertex (point)
43 * geometry and the sketch feature.
44 * point - Called as each point is added. Receives the new point geometry.
45 * done - Called when the point drawing is finished. The callback will
46 * recieve a single argument, the polygon geometry.
47 * cancel - Called when the handler is deactivated while drawing. The
48 * cancel callback will receive a geometry.
50 initialize: function(control, callbacks, options) {
51 OpenLayers.Handler.Path.prototype.initialize.apply(this, arguments);
55 * Method: createFeature
56 * Add temporary geometries
59 * pixel - {<OpenLayers.Pixel>} The initial pixel location for the new
62 createFeature: function(pixel) {
63 var lonlat = this.control.map.getLonLatFromPixel(pixel);
64 this.point = new OpenLayers.Feature.Vector(
65 new OpenLayers.Geometry.Point(lonlat.lon, lonlat.lat)
67 this.line = new OpenLayers.Feature.Vector(
68 new OpenLayers.Geometry.LinearRing([this.point.geometry])
70 this.polygon = new OpenLayers.Feature.Vector(
71 new OpenLayers.Geometry.Polygon([this.line.geometry])
73 this.callback("create", [this.point.geometry, this.getSketch()]);
74 this.point.geometry.clearBounds();
75 this.layer.addFeatures([this.polygon, this.point], {silent: true});
79 * Method: destroyFeature
80 * Destroy temporary geometries
82 destroyFeature: function() {
83 OpenLayers.Handler.Path.prototype.destroyFeature.apply(this);
89 * Render geometries on the temporary layer.
91 drawFeature: function() {
92 this.layer.drawFeature(this.polygon, this.style);
93 this.layer.drawFeature(this.point, this.style);
98 * Return the sketch feature.
101 * {<OpenLayers.Feature.Vector>}
103 getSketch: function() {
108 * Method: getGeometry
109 * Return the sketch geometry. If <multi> is true, this will return
110 * a multi-part geometry.
113 * {<OpenLayers.Geometry.Polygon>}
115 getGeometry: function() {
116 var geometry = this.polygon && this.polygon.geometry;
117 if(geometry && this.multi) {
118 geometry = new OpenLayers.Geometry.MultiPolygon([geometry]);
125 * Handle double-clicks. Finish the geometry and send it back
131 dblclick: function(evt) {
132 if(!this.freehandMode(evt)) {
133 // remove the penultimate point
134 var index = this.line.geometry.components.length - 2;
135 this.line.geometry.removeComponent(this.line.geometry.components[index]);
142 CLASS_NAME: "OpenLayers.Handler.Polygon"