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
|
/**
|
8
|
* @requires OpenLayers/Control.js
|
9
|
* @requires OpenLayers/Events/buttonclick.js
|
10
|
*/
|
11
|
|
12
|
/**
|
13
|
* Class: OpenLayers.Control.PanZoom
|
14
|
* The PanZoom is a visible control, composed of a
|
15
|
* <OpenLayers.Control.PanPanel> and a <OpenLayers.Control.ZoomPanel>. By
|
16
|
* default it is drawn in the upper left corner of the map.
|
17
|
*
|
18
|
* Inherits from:
|
19
|
* - <OpenLayers.Control>
|
20
|
*/
|
21
|
OpenLayers.Control.PanZoom = OpenLayers.Class(OpenLayers.Control, {
|
22
|
|
23
|
/**
|
24
|
* APIProperty: slideFactor
|
25
|
* {Integer} Number of pixels by which we'll pan the map in any direction
|
26
|
* on clicking the arrow buttons. If you want to pan by some ratio
|
27
|
* of the map dimensions, use <slideRatio> instead.
|
28
|
*/
|
29
|
slideFactor: 50,
|
30
|
|
31
|
/**
|
32
|
* APIProperty: slideRatio
|
33
|
* {Number} The fraction of map width/height by which we'll pan the map
|
34
|
* on clicking the arrow buttons. Default is null. If set, will
|
35
|
* override <slideFactor>. E.g. if slideRatio is .5, then the Pan Up
|
36
|
* button will pan up half the map height.
|
37
|
*/
|
38
|
slideRatio: null,
|
39
|
|
40
|
/**
|
41
|
* Property: buttons
|
42
|
* {Array(DOMElement)} Array of Button Divs
|
43
|
*/
|
44
|
buttons: null,
|
45
|
|
46
|
/**
|
47
|
* Property: position
|
48
|
* {<OpenLayers.Pixel>}
|
49
|
*/
|
50
|
position: null,
|
51
|
|
52
|
/**
|
53
|
* Constructor: OpenLayers.Control.PanZoom
|
54
|
*
|
55
|
* Parameters:
|
56
|
* options - {Object}
|
57
|
*/
|
58
|
initialize: function(options) {
|
59
|
this.position = new OpenLayers.Pixel(OpenLayers.Control.PanZoom.X,
|
60
|
OpenLayers.Control.PanZoom.Y);
|
61
|
OpenLayers.Control.prototype.initialize.apply(this, arguments);
|
62
|
},
|
63
|
|
64
|
/**
|
65
|
* APIMethod: destroy
|
66
|
*/
|
67
|
destroy: function() {
|
68
|
if (this.map) {
|
69
|
this.map.events.unregister("buttonclick", this, this.onButtonClick);
|
70
|
}
|
71
|
this.removeButtons();
|
72
|
this.buttons = null;
|
73
|
this.position = null;
|
74
|
OpenLayers.Control.prototype.destroy.apply(this, arguments);
|
75
|
},
|
76
|
|
77
|
/**
|
78
|
* Method: setMap
|
79
|
*
|
80
|
* Properties:
|
81
|
* map - {<OpenLayers.Map>}
|
82
|
*/
|
83
|
setMap: function(map) {
|
84
|
OpenLayers.Control.prototype.setMap.apply(this, arguments);
|
85
|
this.map.events.register("buttonclick", this, this.onButtonClick);
|
86
|
},
|
87
|
|
88
|
/**
|
89
|
* Method: draw
|
90
|
*
|
91
|
* Parameters:
|
92
|
* px - {<OpenLayers.Pixel>}
|
93
|
*
|
94
|
* Returns:
|
95
|
* {DOMElement} A reference to the container div for the PanZoom control.
|
96
|
*/
|
97
|
draw: function(px) {
|
98
|
// initialize our internal div
|
99
|
OpenLayers.Control.prototype.draw.apply(this, arguments);
|
100
|
px = this.position;
|
101
|
|
102
|
// place the controls
|
103
|
this.buttons = [];
|
104
|
|
105
|
var sz = {w: 18, h: 18};
|
106
|
var centered = new OpenLayers.Pixel(px.x+sz.w/2, px.y);
|
107
|
|
108
|
this._addButton("panup", "north-mini.png", centered, sz);
|
109
|
px.y = centered.y+sz.h;
|
110
|
this._addButton("panleft", "west-mini.png", px, sz);
|
111
|
this._addButton("panright", "east-mini.png", px.add(sz.w, 0), sz);
|
112
|
this._addButton("pandown", "south-mini.png",
|
113
|
centered.add(0, sz.h*2), sz);
|
114
|
this._addButton("zoomin", "zoom-plus-mini.png",
|
115
|
centered.add(0, sz.h*3+5), sz);
|
116
|
this._addButton("zoomworld", "zoom-world-mini.png",
|
117
|
centered.add(0, sz.h*4+5), sz);
|
118
|
this._addButton("zoomout", "zoom-minus-mini.png",
|
119
|
centered.add(0, sz.h*5+5), sz);
|
120
|
return this.div;
|
121
|
},
|
122
|
|
123
|
/**
|
124
|
* Method: _addButton
|
125
|
*
|
126
|
* Parameters:
|
127
|
* id - {String}
|
128
|
* img - {String}
|
129
|
* xy - {<OpenLayers.Pixel>}
|
130
|
* sz - {<OpenLayers.Size>}
|
131
|
*
|
132
|
* Returns:
|
133
|
* {DOMElement} A Div (an alphaImageDiv, to be precise) that contains the
|
134
|
* image of the button, and has all the proper event handlers set.
|
135
|
*/
|
136
|
_addButton:function(id, img, xy, sz) {
|
137
|
var imgLocation = OpenLayers.Util.getImageLocation(img);
|
138
|
var btn = OpenLayers.Util.createAlphaImageDiv(
|
139
|
this.id + "_" + id,
|
140
|
xy, sz, imgLocation, "absolute");
|
141
|
btn.style.cursor = "pointer";
|
142
|
//we want to add the outer div
|
143
|
this.div.appendChild(btn);
|
144
|
btn.action = id;
|
145
|
btn.className = "olButton";
|
146
|
|
147
|
//we want to remember/reference the outer div
|
148
|
this.buttons.push(btn);
|
149
|
return btn;
|
150
|
},
|
151
|
|
152
|
/**
|
153
|
* Method: _removeButton
|
154
|
*
|
155
|
* Parameters:
|
156
|
* btn - {Object}
|
157
|
*/
|
158
|
_removeButton: function(btn) {
|
159
|
this.div.removeChild(btn);
|
160
|
OpenLayers.Util.removeItem(this.buttons, btn);
|
161
|
},
|
162
|
|
163
|
/**
|
164
|
* Method: removeButtons
|
165
|
*/
|
166
|
removeButtons: function() {
|
167
|
for(var i=this.buttons.length-1; i>=0; --i) {
|
168
|
this._removeButton(this.buttons[i]);
|
169
|
}
|
170
|
},
|
171
|
|
172
|
/**
|
173
|
* Method: onButtonClick
|
174
|
*
|
175
|
* Parameters:
|
176
|
* evt - {Event}
|
177
|
*/
|
178
|
onButtonClick: function(evt) {
|
179
|
var btn = evt.buttonElement;
|
180
|
switch (btn.action) {
|
181
|
case "panup":
|
182
|
this.map.pan(0, -this.getSlideFactor("h"));
|
183
|
break;
|
184
|
case "pandown":
|
185
|
this.map.pan(0, this.getSlideFactor("h"));
|
186
|
break;
|
187
|
case "panleft":
|
188
|
this.map.pan(-this.getSlideFactor("w"), 0);
|
189
|
break;
|
190
|
case "panright":
|
191
|
this.map.pan(this.getSlideFactor("w"), 0);
|
192
|
break;
|
193
|
case "zoomin":
|
194
|
this.map.zoomIn();
|
195
|
break;
|
196
|
case "zoomout":
|
197
|
this.map.zoomOut();
|
198
|
break;
|
199
|
case "zoomworld":
|
200
|
this.map.zoomToMaxExtent();
|
201
|
break;
|
202
|
}
|
203
|
},
|
204
|
|
205
|
/**
|
206
|
* Method: getSlideFactor
|
207
|
*
|
208
|
* Parameters:
|
209
|
* dim - {String} "w" or "h" (for width or height).
|
210
|
*
|
211
|
* Returns:
|
212
|
* {Number} The slide factor for panning in the requested direction.
|
213
|
*/
|
214
|
getSlideFactor: function(dim) {
|
215
|
return this.slideRatio ?
|
216
|
this.map.getSize()[dim] * this.slideRatio :
|
217
|
this.slideFactor;
|
218
|
},
|
219
|
|
220
|
CLASS_NAME: "OpenLayers.Control.PanZoom"
|
221
|
});
|
222
|
|
223
|
/**
|
224
|
* Constant: X
|
225
|
* {Integer}
|
226
|
*/
|
227
|
OpenLayers.Control.PanZoom.X = 4;
|
228
|
|
229
|
/**
|
230
|
* Constant: Y
|
231
|
* {Integer}
|
232
|
*/
|
233
|
OpenLayers.Control.PanZoom.Y = 4;
|