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/Layer.js |
---|
8 | */ |
---|
9 | |
---|
10 | /** |
---|
11 | * Class: OpenLayers.Layer.FixedZoomLevels |
---|
12 | * Some Layers will already have established zoom levels (like google |
---|
13 | * or ve). Instead of trying to determine them and populate a resolutions[] |
---|
14 | * Array with those values, we will hijack the resolution functionality |
---|
15 | * here. |
---|
16 | * |
---|
17 | * When you subclass FixedZoomLevels: |
---|
18 | * |
---|
19 | * The initResolutions() call gets nullified, meaning no resolutions[] array |
---|
20 | * is set up. Which would be a big problem getResolution() in Layer, since |
---|
21 | * it merely takes map.zoom and indexes into resolutions[]... but.... |
---|
22 | * |
---|
23 | * The getResolution() call is also overridden. Instead of using the |
---|
24 | * resolutions[] array, we simply calculate the current resolution based |
---|
25 | * on the current extent and the current map size. But how will we be able |
---|
26 | * to calculate the current extent without knowing the resolution...? |
---|
27 | * |
---|
28 | * The getExtent() function is also overridden. Instead of calculating extent |
---|
29 | * based on the center point and the current resolution, we instead |
---|
30 | * calculate the extent by getting the lonlats at the top-left and |
---|
31 | * bottom-right by using the getLonLatFromViewPortPx() translation function, |
---|
32 | * taken from the pixel locations (0,0) and the size of the map. But how |
---|
33 | * will we be able to do lonlat-px translation without resolution....? |
---|
34 | * |
---|
35 | * The getZoomForResolution() method is overridden. Instead of indexing into |
---|
36 | * the resolutions[] array, we call OpenLayers.Layer.getExent(), passing in |
---|
37 | * the desired resolution. With this extent, we then call getZoomForExtent() |
---|
38 | * |
---|
39 | * |
---|
40 | * Whenever you implement a layer using OpenLayers.Layer.FixedZoomLevels, |
---|
41 | * it is your responsibility to provide the following three functions: |
---|
42 | * |
---|
43 | * - getLonLatFromViewPortPx |
---|
44 | * - getViewPortPxFromLonLat |
---|
45 | * - getZoomForExtent |
---|
46 | * |
---|
47 | * ...those three functions should generally be provided by any reasonable |
---|
48 | * API that you might be working from. |
---|
49 | * |
---|
50 | */ |
---|
51 | OpenLayers.Layer.FixedZoomLevels = OpenLayers.Class({ |
---|
52 | |
---|
53 | /********************************************************/ |
---|
54 | /* */ |
---|
55 | /* Baselayer Functions */ |
---|
56 | /* */ |
---|
57 | /* The following functions must all be implemented */ |
---|
58 | /* by all base layers */ |
---|
59 | /* */ |
---|
60 | /********************************************************/ |
---|
61 | |
---|
62 | /** |
---|
63 | * Constructor: OpenLayers.Layer.FixedZoomLevels |
---|
64 | * Create a new fixed zoom levels layer. |
---|
65 | */ |
---|
66 | initialize: function() { |
---|
67 | //this class is only just to add the following functions... |
---|
68 | // nothing to actually do here... but it is probably a good |
---|
69 | // idea to have layers that use these functions call this |
---|
70 | // inititalize() anyways, in case at some point we decide we |
---|
71 | // do want to put some functionality or state in here. |
---|
72 | }, |
---|
73 | |
---|
74 | /** |
---|
75 | * Method: initResolutions |
---|
76 | * Populate the resolutions array |
---|
77 | */ |
---|
78 | initResolutions: function() { |
---|
79 | |
---|
80 | var props = new Array('minZoomLevel', 'maxZoomLevel', 'numZoomLevels'); |
---|
81 | |
---|
82 | for(var i=0, len=props.length; i<len; i++) { |
---|
83 | var property = props[i]; |
---|
84 | this[property] = (this.options[property] != null) |
---|
85 | ? this.options[property] |
---|
86 | : this.map[property]; |
---|
87 | } |
---|
88 | |
---|
89 | if ( (this.minZoomLevel == null) || |
---|
90 | (this.minZoomLevel < this.MIN_ZOOM_LEVEL) ){ |
---|
91 | this.minZoomLevel = this.MIN_ZOOM_LEVEL; |
---|
92 | } |
---|
93 | |
---|
94 | // |
---|
95 | // At this point, we know what the minimum desired zoom level is, and |
---|
96 | // we must calculate the total number of zoom levels. |
---|
97 | // |
---|
98 | // Because we allow for the setting of either the 'numZoomLevels' |
---|
99 | // or the 'maxZoomLevel' properties... on either the layer or the |
---|
100 | // map, we have to define some rules to see which we take into |
---|
101 | // account first in this calculation. |
---|
102 | // |
---|
103 | // The following is the precedence list for these properties: |
---|
104 | // |
---|
105 | // (1) numZoomLevels set on layer |
---|
106 | // (2) maxZoomLevel set on layer |
---|
107 | // (3) numZoomLevels set on map |
---|
108 | // (4) maxZoomLevel set on map* |
---|
109 | // (5) none of the above* |
---|
110 | // |
---|
111 | // *Note that options (4) and (5) are only possible if the user |
---|
112 | // _explicitly_ sets the 'numZoomLevels' property on the map to |
---|
113 | // null, since it is set by default to 16. |
---|
114 | // |
---|
115 | |
---|
116 | // |
---|
117 | // Note to future: In 3.0, I think we should remove the default |
---|
118 | // value of 16 for map.numZoomLevels. Rather, I think that value |
---|
119 | // should be set as a default on the Layer.WMS class. If someone |
---|
120 | // creates a 3rd party layer and does not specify any 'minZoomLevel', |
---|
121 | // 'maxZoomLevel', or 'numZoomLevels', and has not explicitly |
---|
122 | // specified any of those on the map object either.. then I think |
---|
123 | // it is fair to say that s/he wants all the zoom levels available. |
---|
124 | // |
---|
125 | // By making map.numZoomLevels *null* by default, that will be the |
---|
126 | // case. As it is, I don't feel comfortable changing that right now |
---|
127 | // as it would be a glaring API change and actually would probably |
---|
128 | // break many peoples' codes. |
---|
129 | // |
---|
130 | |
---|
131 | //the number of zoom levels we'd like to have. |
---|
132 | var desiredZoomLevels; |
---|
133 | |
---|
134 | //this is the maximum number of zoom levels the layer will allow, |
---|
135 | // given the specified starting minimum zoom level. |
---|
136 | var limitZoomLevels = this.MAX_ZOOM_LEVEL - this.minZoomLevel + 1; |
---|
137 | |
---|
138 | if ( ((this.options.numZoomLevels == null) && |
---|
139 | (this.options.maxZoomLevel != null)) // (2) |
---|
140 | || |
---|
141 | ((this.numZoomLevels == null) && |
---|
142 | (this.maxZoomLevel != null)) // (4) |
---|
143 | ) { |
---|
144 | //calculate based on specified maxZoomLevel (on layer or map) |
---|
145 | desiredZoomLevels = this.maxZoomLevel - this.minZoomLevel + 1; |
---|
146 | } else { |
---|
147 | //calculate based on specified numZoomLevels (on layer or map) |
---|
148 | // this covers cases (1) and (3) |
---|
149 | desiredZoomLevels = this.numZoomLevels; |
---|
150 | } |
---|
151 | |
---|
152 | if (desiredZoomLevels != null) { |
---|
153 | //Now that we know what we would *like* the number of zoom levels |
---|
154 | // to be, based on layer or map options, we have to make sure that |
---|
155 | // it does not conflict with the actual limit, as specified by |
---|
156 | // the constants on the layer itself (and calculated into the |
---|
157 | // 'limitZoomLevels' variable). |
---|
158 | this.numZoomLevels = Math.min(desiredZoomLevels, limitZoomLevels); |
---|
159 | } else { |
---|
160 | // case (5) -- neither 'numZoomLevels' not 'maxZoomLevel' was |
---|
161 | // set on either the layer or the map. So we just use the |
---|
162 | // maximum limit as calculated by the layer's constants. |
---|
163 | this.numZoomLevels = limitZoomLevels; |
---|
164 | } |
---|
165 | |
---|
166 | //now that the 'numZoomLevels' is appropriately, safely set, |
---|
167 | // we go back and re-calculate the 'maxZoomLevel'. |
---|
168 | this.maxZoomLevel = this.minZoomLevel + this.numZoomLevels - 1; |
---|
169 | |
---|
170 | if (this.RESOLUTIONS != null) { |
---|
171 | var resolutionsIndex = 0; |
---|
172 | this.resolutions = []; |
---|
173 | for(var i= this.minZoomLevel; i <= this.maxZoomLevel; i++) { |
---|
174 | this.resolutions[resolutionsIndex++] = this.RESOLUTIONS[i]; |
---|
175 | } |
---|
176 | this.maxResolution = this.resolutions[0]; |
---|
177 | this.minResolution = this.resolutions[this.resolutions.length - 1]; |
---|
178 | } |
---|
179 | }, |
---|
180 | |
---|
181 | /** |
---|
182 | * APIMethod: getResolution |
---|
183 | * Get the current map resolution |
---|
184 | * |
---|
185 | * Returns: |
---|
186 | * {Float} Map units per Pixel |
---|
187 | */ |
---|
188 | getResolution: function() { |
---|
189 | |
---|
190 | if (this.resolutions != null) { |
---|
191 | return OpenLayers.Layer.prototype.getResolution.apply(this, arguments); |
---|
192 | } else { |
---|
193 | var resolution = null; |
---|
194 | |
---|
195 | var viewSize = this.map.getSize(); |
---|
196 | var extent = this.getExtent(); |
---|
197 | |
---|
198 | if ((viewSize != null) && (extent != null)) { |
---|
199 | resolution = Math.max( extent.getWidth() / viewSize.w, |
---|
200 | extent.getHeight() / viewSize.h ); |
---|
201 | } |
---|
202 | return resolution; |
---|
203 | } |
---|
204 | }, |
---|
205 | |
---|
206 | /** |
---|
207 | * APIMethod: getExtent |
---|
208 | * Calculates using px-> lonlat translation functions on tl and br |
---|
209 | * corners of viewport |
---|
210 | * |
---|
211 | * Returns: |
---|
212 | * {<OpenLayers.Bounds>} A Bounds object which represents the lon/lat |
---|
213 | * bounds of the current viewPort. |
---|
214 | */ |
---|
215 | getExtent: function () { |
---|
216 | var extent = null; |
---|
217 | |
---|
218 | |
---|
219 | var size = this.map.getSize(); |
---|
220 | |
---|
221 | var tlPx = new OpenLayers.Pixel(0,0); |
---|
222 | var tlLL = this.getLonLatFromViewPortPx(tlPx); |
---|
223 | |
---|
224 | var brPx = new OpenLayers.Pixel(size.w, size.h); |
---|
225 | var brLL = this.getLonLatFromViewPortPx(brPx); |
---|
226 | |
---|
227 | if ((tlLL != null) && (brLL != null)) { |
---|
228 | extent = new OpenLayers.Bounds(tlLL.lon, |
---|
229 | brLL.lat, |
---|
230 | brLL.lon, |
---|
231 | tlLL.lat); |
---|
232 | } |
---|
233 | |
---|
234 | return extent; |
---|
235 | }, |
---|
236 | |
---|
237 | /** |
---|
238 | * Method: getZoomForResolution |
---|
239 | * Get the zoom level for a given resolution |
---|
240 | * |
---|
241 | * Parameters: |
---|
242 | * resolution - {Float} |
---|
243 | * |
---|
244 | * Returns: |
---|
245 | * {Integer} A suitable zoom level for the specified resolution. |
---|
246 | * If no baselayer is set, returns null. |
---|
247 | */ |
---|
248 | getZoomForResolution: function(resolution) { |
---|
249 | |
---|
250 | if (this.resolutions != null) { |
---|
251 | return OpenLayers.Layer.prototype.getZoomForResolution.apply(this, arguments); |
---|
252 | } else { |
---|
253 | var extent = OpenLayers.Layer.prototype.getExtent.apply(this, []); |
---|
254 | return this.getZoomForExtent(extent); |
---|
255 | } |
---|
256 | }, |
---|
257 | |
---|
258 | |
---|
259 | |
---|
260 | |
---|
261 | /********************************************************/ |
---|
262 | /* */ |
---|
263 | /* Translation Functions */ |
---|
264 | /* */ |
---|
265 | /* The following functions translate GMaps and OL */ |
---|
266 | /* formats for Pixel, LonLat, Bounds, and Zoom */ |
---|
267 | /* */ |
---|
268 | /********************************************************/ |
---|
269 | |
---|
270 | |
---|
271 | // |
---|
272 | // TRANSLATION: MapObject Zoom <-> OpenLayers Zoom |
---|
273 | // |
---|
274 | |
---|
275 | /** |
---|
276 | * Method: getOLZoomFromMapObjectZoom |
---|
277 | * Get the OL zoom index from the map object zoom level |
---|
278 | * |
---|
279 | * Parameters: |
---|
280 | * moZoom - {Integer} |
---|
281 | * |
---|
282 | * Returns: |
---|
283 | * {Integer} An OpenLayers Zoom level, translated from the passed in zoom |
---|
284 | * Returns null if null value is passed in |
---|
285 | */ |
---|
286 | getOLZoomFromMapObjectZoom: function(moZoom) { |
---|
287 | var zoom = null; |
---|
288 | if (moZoom != null) { |
---|
289 | zoom = moZoom - this.minZoomLevel; |
---|
290 | } |
---|
291 | return zoom; |
---|
292 | }, |
---|
293 | |
---|
294 | /** |
---|
295 | * Method: getMapObjectZoomFromOLZoom |
---|
296 | * Get the map object zoom level from the OL zoom level |
---|
297 | * |
---|
298 | * Parameters: |
---|
299 | * olZoom - {Integer} |
---|
300 | * |
---|
301 | * Returns: |
---|
302 | * {Integer} A MapObject level, translated from the passed in olZoom |
---|
303 | * Returns null if null value is passed in |
---|
304 | */ |
---|
305 | getMapObjectZoomFromOLZoom: function(olZoom) { |
---|
306 | var zoom = null; |
---|
307 | if (olZoom != null) { |
---|
308 | zoom = olZoom + this.minZoomLevel; |
---|
309 | } |
---|
310 | return zoom; |
---|
311 | }, |
---|
312 | |
---|
313 | CLASS_NAME: "OpenLayers.Layer.FixedZoomLevels" |
---|
314 | }); |
---|
315 | |
---|