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>}
34
* callbacks - {Object} An object with a 'done' property whos value is
35
* a function to be called when the path drawing is
36
* finished. The callback should expect to recieve a
37
* single argument, the polygon geometry.
38
* If the callbacks object contains a 'point'
39
* property, this function will be sent each point
40
* as they are added. If the callbacks object contains
41
* a 'cancel' property, this function will be called when
42
* the handler is deactivated while drawing. The cancel
43
* should expect to receive a geometry.
46
initialize: function(control, callbacks, options) {
47
OpenLayers.Handler.Path.prototype.initialize.apply(this, arguments);
51
* Method: createFeature
52
* Add temporary geometries
54
createFeature: function() {
55
this.polygon = new OpenLayers.Feature.Vector(
56
new OpenLayers.Geometry.Polygon());
57
this.line = new OpenLayers.Feature.Vector(
58
new OpenLayers.Geometry.LinearRing());
59
this.polygon.geometry.addComponent(this.line.geometry);
60
this.point = new OpenLayers.Feature.Vector(
61
new OpenLayers.Geometry.Point());
62
this.layer.addFeatures([this.polygon, this.point], {silent: true});
66
* Method: destroyFeature
67
* Destroy temporary geometries
69
destroyFeature: function() {
70
OpenLayers.Handler.Path.prototype.destroyFeature.apply(this);
75
* Method: modifyFeature
76
* Modify the existing geometry given the new point
79
modifyFeature: function() {
80
var index = this.line.geometry.components.length - 2;
81
this.line.geometry.components[index].x = this.point.geometry.x;
82
this.line.geometry.components[index].y = this.point.geometry.y;
83
this.line.geometry.components[index].clearBounds();
88
* Render geometries on the temporary layer.
90
drawFeature: function() {
91
this.layer.drawFeature(this.polygon, this.style);
92
this.layer.drawFeature(this.point, this.style);
97
* Return the sketch geometry. If <multi> is true, this will return
98
* a multi-part geometry.
101
* {<OpenLayers.Geometry.Polygon>}
103
getGeometry: function() {
104
var geometry = this.polygon.geometry;
106
geometry = new OpenLayers.Geometry.MultiPolygon([geometry]);
113
* Handle double-clicks. Finish the geometry and send it back
119
dblclick: function(evt) {
120
if(!this.freehandMode(evt)) {
121
// remove the penultimate point
122
var index = this.line.geometry.components.length - 2;
123
this.line.geometry.removeComponent(this.line.geometry.components[index]);
132
CLASS_NAME: "OpenLayers.Handler.Polygon"