]> dev.renevier.net Git - syp.git/blob - openlayers/lib/OpenLayers/Layer/Yahoo.js
fixes notices
[syp.git] / openlayers / lib / OpenLayers / Layer / Yahoo.js
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. */
4
5
6 /**
7  * @requires OpenLayers/Layer/EventPane.js
8  * @requires OpenLayers/Layer/FixedZoomLevels.js
9  */
10
11 /**
12  * Class: OpenLayers.Layer.Yahoo
13  * 
14  * Inherits from:
15  *  - <OpenLayers.Layer.EventPane>
16  *  - <OpenLayers.Layer.FixedZoomLevels>
17  */
18 OpenLayers.Layer.Yahoo = OpenLayers.Class(
19   OpenLayers.Layer.EventPane, OpenLayers.Layer.FixedZoomLevels, {
20     
21     /** 
22      * Constant: MIN_ZOOM_LEVEL
23      * {Integer} 0 
24      */
25     MIN_ZOOM_LEVEL: 0,
26     
27     /** 
28      * Constant: MAX_ZOOM_LEVEL
29      * {Integer} 17
30      */
31     MAX_ZOOM_LEVEL: 17,
32
33     /** 
34      * Constant: RESOLUTIONS
35      * {Array(Float)} Hardcode these resolutions so that they are more closely
36      *                tied with the standard wms projection
37      */
38     RESOLUTIONS: [
39         1.40625, 
40         0.703125, 
41         0.3515625, 
42         0.17578125, 
43         0.087890625, 
44         0.0439453125,
45         0.02197265625, 
46         0.010986328125, 
47         0.0054931640625, 
48         0.00274658203125, 
49         0.001373291015625, 
50         0.0006866455078125, 
51         0.00034332275390625, 
52         0.000171661376953125, 
53         0.0000858306884765625, 
54         0.00004291534423828125,
55         0.00002145767211914062,
56         0.00001072883605957031
57     ],
58
59     /**
60      * APIProperty: type
61      * {YahooMapType}
62      */
63     type: null,
64     
65     /**
66      * APIProperty: sphericalMercator
67      * {Boolean} Should the map act as a mercator-projected map? This will
68      * cause all interactions with the map to be in the actual map projection,
69      * which allows support for vector drawing, overlaying other maps, etc. 
70      */
71     sphericalMercator: false, 
72
73     /** 
74      * Constructor: OpenLayers.Layer.Yahoo
75      * 
76      * Parameters:
77      * name - {String}
78      * options - {Object}
79      */
80     initialize: function(name, options) {
81         OpenLayers.Layer.EventPane.prototype.initialize.apply(this, arguments);
82         OpenLayers.Layer.FixedZoomLevels.prototype.initialize.apply(this, 
83                                                                     arguments);
84         if(this.sphericalMercator) {
85             OpenLayers.Util.extend(this, OpenLayers.Layer.SphericalMercator);
86             this.initMercatorParameters();
87         }
88     },
89     
90     /**
91      * Method: loadMapObject
92      */
93     loadMapObject:function() {
94         try { //do not crash! 
95             var size = this.getMapObjectSizeFromOLSize(this.map.getSize());
96             this.mapObject = new YMap(this.div, this.type, size);
97             this.mapObject.disableKeyControls();
98             this.mapObject.disableDragMap();
99
100             //can we do smooth panning? (moveByXY is not an API function)
101             if ( !this.mapObject.moveByXY || 
102                  (typeof this.mapObject.moveByXY != "function" ) ) {
103
104                 this.dragPanMapObject = null;
105             }                
106         } catch(e) {}
107     },
108
109     /**
110      * Method: onMapResize
111      * 
112      */
113     onMapResize: function() {
114         try {
115             var size = this.getMapObjectSizeFromOLSize(this.map.getSize());
116             this.mapObject.resizeTo(size);
117         } catch(e) {}     
118     },    
119     
120     
121     /** 
122      * APIMethod: setMap
123      * Overridden from EventPane because we need to remove this yahoo event
124      *     pane which prohibits our drag and drop, and we can only do this 
125      *     once the map has been loaded and centered.
126      * 
127      * Parameters:
128      * map - {<OpenLayers.Map>}
129      */
130     setMap: function(map) {
131         OpenLayers.Layer.EventPane.prototype.setMap.apply(this, arguments);
132
133         this.map.events.register("moveend", this, this.fixYahooEventPane);
134     },
135
136     /** 
137      * Method: fixYahooEventPane
138      * The map has been centered, so the mysterious yahoo eventpane has been
139      *     added. we remove it so that it doesnt mess with *our* event pane.
140      */
141     fixYahooEventPane: function() {
142         var yahooEventPane = OpenLayers.Util.getElement("ygddfdiv");
143         if (yahooEventPane != null) {
144             if (yahooEventPane.parentNode != null) {
145                 yahooEventPane.parentNode.removeChild(yahooEventPane);
146             }
147             this.map.events.unregister("moveend", this, 
148                                        this.fixYahooEventPane);
149         }
150     },
151
152     /** 
153      * APIMethod: getWarningHTML
154      * 
155      * Returns: 
156      * {String} String with information on why layer is broken, how to get
157      *          it working.
158      */
159     getWarningHTML:function() {
160         return OpenLayers.i18n(
161             "getLayerWarning", {'layerType':'Yahoo', 'layerLib':'Yahoo'}
162         );
163     },
164
165   /********************************************************/
166   /*                                                      */
167   /*             Translation Functions                    */
168   /*                                                      */
169   /*    The following functions translate GMaps and OL    */ 
170   /*     formats for Pixel, LonLat, Bounds, and Zoom      */
171   /*                                                      */
172   /********************************************************/
173
174
175   //
176   // TRANSLATION: MapObject Zoom <-> OpenLayers Zoom
177   //
178   
179     /**
180      * APIMethod: getOLZoomFromMapObjectZoom
181      * 
182      * Parameters:
183      * gZoom - {Integer}
184      * 
185      * Returns:
186      * {Integer} An OpenLayers Zoom level, translated from the passed in gZoom
187      *           Returns null if null value is passed in.
188      */
189     getOLZoomFromMapObjectZoom: function(moZoom) {
190         var zoom = null;
191         if (moZoom != null) {
192             zoom = OpenLayers.Layer.FixedZoomLevels.prototype.getOLZoomFromMapObjectZoom.apply(this, [moZoom]);
193             zoom = 18 - zoom;
194         }
195         return zoom;
196     },
197     
198     /**
199      * APIMethod: getMapObjectZoomFromOLZoom
200      * 
201      * Parameters:
202      * olZoom - {Integer}
203      * 
204      * Returns:
205      * {Integer} A MapObject level, translated from the passed in olZoom
206      *           Returns null if null value is passed in
207      */
208     getMapObjectZoomFromOLZoom: function(olZoom) {
209         var zoom = null; 
210         if (olZoom != null) {
211             zoom = OpenLayers.Layer.FixedZoomLevels.prototype.getMapObjectZoomFromOLZoom.apply(this, [olZoom]);
212             zoom = 18 - zoom;
213         }
214         return zoom;
215     },
216
217     /************************************
218      *                                  *
219      *   MapObject Interface Controls   *
220      *                                  *
221      ************************************/
222
223
224   // Get&Set Center, Zoom
225
226     /** 
227      * APIMethod: setMapObjectCenter
228      * Set the mapObject to the specified center and zoom
229      * 
230      * Parameters:
231      * center - {Object} MapObject LonLat format
232      * zoom - {int} MapObject zoom format
233      */
234     setMapObjectCenter: function(center, zoom) {
235         this.mapObject.drawZoomAndCenter(center, zoom); 
236     },
237    
238     /**
239      * APIMethod: getMapObjectCenter
240      * 
241      * Returns: 
242      * {Object} The mapObject's current center in Map Object format
243      */
244     getMapObjectCenter: function() {
245         return this.mapObject.getCenterLatLon();
246     },
247
248     /**
249      * APIMethod: dragPanMapObject
250      * 
251      * Parameters:
252      * dX - {Integer}
253      * dY - {Integer}
254      */
255     dragPanMapObject: function(dX, dY) {
256         this.mapObject.moveByXY({
257             'x': -dX,
258             'y': dY
259         });
260     },
261     
262     /** 
263      * APIMethod: getMapObjectZoom
264      * 
265      * Returns:
266      * {Integer} The mapObject's current zoom, in Map Object format
267      */
268     getMapObjectZoom: function() {
269         return this.mapObject.getZoomLevel();
270     },
271
272
273   // LonLat - Pixel Translation
274   
275     /**
276      * APIMethod: getMapObjectLonLatFromMapObjectPixel
277      * 
278      * Parameters:
279      * moPixel - {Object} MapObject Pixel format
280      * 
281      * Returns:
282      * {Object} MapObject LonLat translated from MapObject Pixel
283      */
284     getMapObjectLonLatFromMapObjectPixel: function(moPixel) {
285         return this.mapObject.convertXYLatLon(moPixel);
286     },
287
288     /**
289      * APIMethod: getMapObjectPixelFromMapObjectLonLat
290      * 
291      * Parameters:
292      * moLonLat - {Object} MapObject LonLat format
293      * 
294      * Returns:
295      * {Object} MapObject Pixel transtlated from MapObject LonLat
296      */
297     getMapObjectPixelFromMapObjectLonLat: function(moLonLat) {
298         return this.mapObject.convertLatLonXY(moLonLat);
299     },
300
301
302     /************************************
303      *                                  *
304      *       MapObject Primitives       *
305      *                                  *
306      ************************************/
307
308
309   // LonLat
310     
311     /**
312      * APIMethod: getLongitudeFromMapObjectLonLat
313      * 
314      * Parameters:
315      * moLonLat - {Object} MapObject LonLat format
316      * 
317      * Returns:
318      * {Float} Longitude of the given MapObject LonLat
319      */
320     getLongitudeFromMapObjectLonLat: function(moLonLat) {
321         return this.sphericalMercator ? 
322             this.forwardMercator(moLonLat.Lon, moLonLat.Lat).lon :
323             moLonLat.Lon;
324     },
325
326     /**
327      * APIMethod: getLatitudeFromMapObjectLonLat
328      * 
329      * Parameters:
330      * moLonLat - {Object} MapObject LonLat format
331      * 
332      * Returns:
333      * {Float} Latitude of the given MapObject LonLat
334      */
335     getLatitudeFromMapObjectLonLat: function(moLonLat) {
336         return this.sphericalMercator ? 
337             this.forwardMercator(moLonLat.Lon, moLonLat.Lat).lat :
338             moLonLat.Lat;
339     },
340
341     /**
342      * APIMethod: getMapObjectLonLatFromLonLat
343      * 
344      * Parameters:
345      * lon - {Float}
346      * lat - {Float}
347      * 
348      * Returns:
349      * {Object} MapObject LonLat built from lon and lat params
350      */
351     getMapObjectLonLatFromLonLat: function(lon, lat) {
352         var yLatLong;
353         if(this.sphericalMercator) {
354             var lonlat = this.inverseMercator(lon, lat);
355             yLatLong = new YGeoPoint(lonlat.lat, lonlat.lon);
356         } else {
357             yLatLong = new YGeoPoint(lat, lon);
358         }
359         return yLatLong;
360     },
361
362   // Pixel
363     
364     /**
365      * APIMethod: getXFromMapObjectPixel
366      * 
367      * Parameters:
368      * moPixel - {Object} MapObject Pixel format
369      * 
370      * Returns:
371      * {Integer} X value of the MapObject Pixel
372      */
373     getXFromMapObjectPixel: function(moPixel) {
374         return moPixel.x;
375     },
376
377     /**
378      * APIMethod: getYFromMapObjectPixel
379      * 
380      * Parameters:
381      * moPixel - {Object} MapObject Pixel format
382      * 
383      * Returns:
384      * {Integer} Y value of the MapObject Pixel
385      */
386     getYFromMapObjectPixel: function(moPixel) {
387         return moPixel.y;
388     },
389
390     /**
391      * APIMethod: getMapObjectPixelFromXY
392      * 
393      * Parameters:
394      * x - {Integer}
395      * y - {Integer}
396      * 
397      * Returns:
398      * {Object} MapObject Pixel from x and y parameters
399      */
400     getMapObjectPixelFromXY: function(x, y) {
401         return new YCoordPoint(x, y);
402     },
403     
404   // Size
405   
406     /**
407      * APIMethod: getMapObjectSizeFromOLSize
408      * 
409      * Parameters:
410      * olSize - {<OpenLayers.Size>}
411      * 
412      * Returns:
413      * {Object} MapObject Size from olSize parameter
414      */
415     getMapObjectSizeFromOLSize: function(olSize) {
416         return new YSize(olSize.w, olSize.h);
417     },
418     
419     CLASS_NAME: "OpenLayers.Layer.Yahoo"
420 });