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