Project

General

Profile

Download (4.56 KB) Statistics
| Branch: | Tag: | Revision:
1
/* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for
2
 * full list of contributors). Published under the 2-clause BSD license.
3
 * See license.txt in the OpenLayers distribution or repository 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
     * APIProperty: keyMask
35
     * {Integer} Zoom only occurs if the keyMask matches the combination of 
36
     *     keys down. Use bitwise operators and one or more of the
37
     *     <OpenLayers.Handler> constants to construct a keyMask. Leave null if 
38
     *     not used mask. Default is null.
39
     */
40
    keyMask: null,
41

    
42
    /**
43
     * APIProperty: alwaysZoom
44
     * {Boolean} Always zoom in/out when box drawn, even if the zoom level does
45
     * not change.
46
     */
47
    alwaysZoom: false,
48
    
49
    /**
50
     * APIProperty: zoomOnClick
51
     * {Boolean} Should we zoom when no box was dragged, i.e. the user only
52
     * clicked? Default is true.
53
     */
54
    zoomOnClick: true,
55

    
56
    /**
57
     * Method: draw
58
     */    
59
    draw: function() {
60
        this.handler = new OpenLayers.Handler.Box( this,
61
                            {done: this.zoomBox}, {keyMask: this.keyMask} );
62
    },
63

    
64
    /**
65
     * Method: zoomBox
66
     *
67
     * Parameters:
68
     * position - {<OpenLayers.Bounds>} or {<OpenLayers.Pixel>}
69
     */
70
    zoomBox: function (position) {
71
        if (position instanceof OpenLayers.Bounds) {
72
            var bounds,
73
                targetCenterPx = position.getCenterPixel();
74
            if (!this.out) {
75
                var minXY = this.map.getLonLatFromPixel({
76
                    x: position.left,
77
                    y: position.bottom
78
                });
79
                var maxXY = this.map.getLonLatFromPixel({
80
                    x: position.right,
81
                    y: position.top
82
                });
83
                bounds = new OpenLayers.Bounds(minXY.lon, minXY.lat,
84
                                               maxXY.lon, maxXY.lat);
85
            } else {
86
                var pixWidth = position.right - position.left;
87
                var pixHeight = position.bottom - position.top;
88
                var zoomFactor = Math.min((this.map.size.h / pixHeight),
89
                    (this.map.size.w / pixWidth));
90
                var extent = this.map.getExtent();
91
                var center = this.map.getLonLatFromPixel(targetCenterPx);
92
                var xmin = center.lon - (extent.getWidth()/2)*zoomFactor;
93
                var xmax = center.lon + (extent.getWidth()/2)*zoomFactor;
94
                var ymin = center.lat - (extent.getHeight()/2)*zoomFactor;
95
                var ymax = center.lat + (extent.getHeight()/2)*zoomFactor;
96
                bounds = new OpenLayers.Bounds(xmin, ymin, xmax, ymax);
97
            }
98
            // always zoom in/out 
99
            var lastZoom = this.map.getZoom(),
100
                size = this.map.getSize(),
101
                centerPx = {x: size.w / 2, y: size.h / 2},
102
                zoom = this.map.getZoomForExtent(bounds),
103
                oldRes = this.map.getResolution(),
104
                newRes = this.map.getResolutionForZoom(zoom);
105
            if (oldRes == newRes) {
106
                this.map.setCenter(this.map.getLonLatFromPixel(targetCenterPx));
107
            } else {
108
              var zoomOriginPx = {
109
                    x: (oldRes * targetCenterPx.x - newRes * centerPx.x) /
110
                        (oldRes - newRes),
111
                    y: (oldRes * targetCenterPx.y - newRes * centerPx.y) /
112
                        (oldRes - newRes)
113
                };
114
                this.map.zoomTo(zoom, zoomOriginPx);
115
            }
116
            if (lastZoom == this.map.getZoom() && this.alwaysZoom == true){ 
117
                this.map.zoomTo(lastZoom + (this.out ? -1 : 1)); 
118
            }
119
        } else if (this.zoomOnClick) { // it's a pixel
120
            if (!this.out) {
121
                this.map.zoomTo(this.map.getZoom() + 1, position);
122
            } else {
123
                this.map.zoomTo(this.map.getZoom() - 1, position);
124
            }
125
        }
126
    },
127

    
128
    CLASS_NAME: "OpenLayers.Control.ZoomBox"
129
});
(41-41/45)