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. */
6 * @requires OpenLayers/Control.js
7 * @requires OpenLayers/Handler/Box.js
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
17 * - <OpenLayers.Control>
19 OpenLayers.Control.ZoomBox = OpenLayers.Class(OpenLayers.Control, {
22 * {OpenLayers.Control.TYPE}
24 type: OpenLayers.Control.TYPE_TOOL,
28 * {Boolean} Should the control be used for zooming out?
33 * Property: alwaysZoom
34 * {Boolean} Always zoom in/out, when box drawed
42 this.handler = new OpenLayers.Handler.Box( this,
43 {done: this.zoomBox}, {keyMask: this.keyMask} );
50 * position - {<OpenLayers.Bounds>} or {<OpenLayers.Pixel>}
52 zoomBox: function (position) {
53 if (position instanceof OpenLayers.Bounds) {
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);
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);
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));
81 } else { // it's a pixel
83 this.map.setCenter(this.map.getLonLatFromPixel(position),
84 this.map.getZoom() + 1);
86 this.map.setCenter(this.map.getLonLatFromPixel(position),
87 this.map.getZoom() - 1);
92 CLASS_NAME: "OpenLayers.Control.ZoomBox"