]> dev.renevier.net Git - syp.git/blob - openlayers/lib/OpenLayers/Control/ZoomBox.js
fixes notices
[syp.git] / openlayers / lib / OpenLayers / Control / ZoomBox.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  * @requires OpenLayers/Control.js
7  * @requires OpenLayers/Handler/Box.js
8  */
9
10 /**
11  * Class: OpenLayers.Control.ZoomBox
12  * The ZoomBox control enables zooming directly to a given extent, by drawing 
13  * a box on the map. The box is drawn by holding down shift, whilst dragging 
14  * the mouse.
15  *
16  * Inherits from:
17  *  - <OpenLayers.Control>
18  */
19 OpenLayers.Control.ZoomBox = OpenLayers.Class(OpenLayers.Control, {
20     /**
21      * Property: type
22      * {OpenLayers.Control.TYPE}
23      */
24     type: OpenLayers.Control.TYPE_TOOL,
25
26     /**
27      * Property: out
28      * {Boolean} Should the control be used for zooming out?
29      */
30     out: false,
31
32     /**
33      * Property: alwaysZoom
34      * {Boolean} Always zoom in/out, when box drawed 
35      */
36     alwaysZoom: false,
37
38     /**
39      * Method: draw
40      */    
41     draw: function() {
42         this.handler = new OpenLayers.Handler.Box( this,
43                             {done: this.zoomBox}, {keyMask: this.keyMask} );
44     },
45
46     /**
47      * Method: zoomBox
48      *
49      * Parameters:
50      * position - {<OpenLayers.Bounds>} or {<OpenLayers.Pixel>}
51      */
52     zoomBox: function (position) {
53         if (position instanceof OpenLayers.Bounds) {
54             if (!this.out) {
55                 var minXY = this.map.getLonLatFromPixel(
56                             new OpenLayers.Pixel(position.left, position.bottom));
57                 var maxXY = this.map.getLonLatFromPixel(
58                             new OpenLayers.Pixel(position.right, position.top));
59                 var bounds = new OpenLayers.Bounds(minXY.lon, minXY.lat,
60                                                maxXY.lon, maxXY.lat);
61             } else {
62                 var pixWidth = Math.abs(position.right-position.left);
63                 var pixHeight = Math.abs(position.top-position.bottom);
64                 var zoomFactor = Math.min((this.map.size.h / pixHeight),
65                     (this.map.size.w / pixWidth));
66                 var extent = this.map.getExtent();
67                 var center = this.map.getLonLatFromPixel(
68                     position.getCenterPixel());
69                 var xmin = center.lon - (extent.getWidth()/2)*zoomFactor;
70                 var xmax = center.lon + (extent.getWidth()/2)*zoomFactor;
71                 var ymin = center.lat - (extent.getHeight()/2)*zoomFactor;
72                 var ymax = center.lat + (extent.getHeight()/2)*zoomFactor;
73                 var bounds = new OpenLayers.Bounds(xmin, ymin, xmax, ymax);
74             }
75             // always zoom in/out 
76             var lastZoom = this.map.getZoom(); 
77             this.map.zoomToExtent(bounds);
78             if (lastZoom == this.map.getZoom() && this.alwaysZoom == true){ 
79                 this.map.zoomTo(lastZoom + (this.out ? -1 : 1)); 
80             }
81         } else { // it's a pixel
82             if (!this.out) {
83                 this.map.setCenter(this.map.getLonLatFromPixel(position),
84                                this.map.getZoom() + 1);
85             } else {
86                 this.map.setCenter(this.map.getLonLatFromPixel(position),
87                                this.map.getZoom() - 1);
88             }
89         }
90     },
91
92     CLASS_NAME: "OpenLayers.Control.ZoomBox"
93 });