]> dev.renevier.net Git - syp.git/blob - openlayers/lib/OpenLayers/Handler/Polygon.js
fixes notices
[syp.git] / openlayers / lib / OpenLayers / Handler / Polygon.js
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. */
4
5
6 /**
7  * @requires OpenLayers/Handler/Path.js
8  * @requires OpenLayers/Geometry/Polygon.js
9  */
10
11 /**
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.
15  *
16  * Inherits from:
17  *  - <OpenLayers.Handler.Path>
18  *  - <OpenLayers.Handler>
19  */
20 OpenLayers.Handler.Polygon = OpenLayers.Class(OpenLayers.Handler.Path, {
21     
22     /**
23      * Parameter: polygon
24      * {<OpenLayers.Feature.Vector>}
25      */
26     polygon: null,
27
28     /**
29      * Constructor: OpenLayers.Handler.Polygon
30      * Create a Polygon Handler.
31      *
32      * Parameters:
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
37      *           handler
38      *
39      * Named callbacks:
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.
49      */
50     initialize: function(control, callbacks, options) {
51         OpenLayers.Handler.Path.prototype.initialize.apply(this, arguments);
52     },
53     
54     /**
55      * Method: createFeature
56      * Add temporary geometries
57      *
58      * Parameters:
59      * pixel - {<OpenLayers.Pixel>} The initial pixel location for the new
60      *     feature.
61      */
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)
66         );
67         this.line = new OpenLayers.Feature.Vector(
68             new OpenLayers.Geometry.LinearRing([this.point.geometry])
69         );
70         this.polygon = new OpenLayers.Feature.Vector(
71             new OpenLayers.Geometry.Polygon([this.line.geometry])
72         );
73         this.callback("create", [this.point.geometry, this.getSketch()]);
74         this.point.geometry.clearBounds();
75         this.layer.addFeatures([this.polygon, this.point], {silent: true});
76     },
77
78     /**
79      * Method: destroyFeature
80      * Destroy temporary geometries
81      */
82     destroyFeature: function() {
83         OpenLayers.Handler.Path.prototype.destroyFeature.apply(this);
84         this.polygon = null;
85     },
86
87     /**
88      * Method: drawFeature
89      * Render geometries on the temporary layer.
90      */
91     drawFeature: function() {
92         this.layer.drawFeature(this.polygon, this.style);
93         this.layer.drawFeature(this.point, this.style);
94     },
95     
96     /**
97      * Method: getSketch
98      * Return the sketch feature.
99      *
100      * Returns:
101      * {<OpenLayers.Feature.Vector>}
102      */
103     getSketch: function() {
104         return this.polygon;
105     },
106
107     /**
108      * Method: getGeometry
109      * Return the sketch geometry.  If <multi> is true, this will return
110      *     a multi-part geometry.
111      *
112      * Returns:
113      * {<OpenLayers.Geometry.Polygon>}
114      */
115     getGeometry: function() {
116         var geometry = this.polygon && this.polygon.geometry;
117         if(geometry && this.multi) {
118             geometry = new OpenLayers.Geometry.MultiPolygon([geometry]);
119         }
120         return geometry;
121     },
122
123     /**
124      * Method: dblclick
125      * Handle double-clicks.  Finish the geometry and send it back
126      * to the control.
127      * 
128      * Parameters:
129      * evt - {Event} 
130      */
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]);
136             this.removePoint();
137             this.finalize();
138         }
139         return false;
140     },
141
142     CLASS_NAME: "OpenLayers.Handler.Polygon"
143 });