]> dev.renevier.net Git - syp.git/blob - openlayers/lib/OpenLayers/Layer/VirtualEarth.js
fixes notices
[syp.git] / openlayers / lib / OpenLayers / Layer / VirtualEarth.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.VirtualEarth
13  * 
14  * Inherits from:
15  *  - <OpenLayers.Layer.EventPane>
16  *  - <OpenLayers.Layer.FixedZoomLevels>
17  */
18 OpenLayers.Layer.VirtualEarth = OpenLayers.Class(
19     OpenLayers.Layer.EventPane,
20     OpenLayers.Layer.FixedZoomLevels, {
21     
22     /** 
23      * Constant: MIN_ZOOM_LEVEL
24      * {Integer} 1 
25      */
26     MIN_ZOOM_LEVEL: 1,
27     
28     /** 
29      * Constant: MAX_ZOOM_LEVEL
30      * {Integer} 17
31      */
32     MAX_ZOOM_LEVEL: 17,
33
34     /** 
35      * Constant: RESOLUTIONS
36      * {Array(Float)} Hardcode these resolutions so that they are more closely
37      *                tied with the standard wms projection
38      */
39     RESOLUTIONS: [
40         1.40625, 
41         0.703125, 
42         0.3515625, 
43         0.17578125, 
44         0.087890625, 
45         0.0439453125,
46         0.02197265625, 
47         0.010986328125, 
48         0.0054931640625, 
49         0.00274658203125,
50         0.001373291015625, 
51         0.0006866455078125, 
52         0.00034332275390625, 
53         0.000171661376953125, 
54         0.0000858306884765625, 
55         0.00004291534423828125,
56         0.00002145767211914062
57     ],
58
59     /**
60      * APIProperty: type
61      * {VEMapType}
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
69      *     projection, which allows support for vector drawing, overlaying
70      *     other maps, etc. 
71      */
72     sphericalMercator: false, 
73
74     /** 
75      * Constructor: OpenLayers.Layer.VirtualEarth
76      * 
77      * Parameters:
78      * name - {String}
79      * options - {Object}
80      */
81     initialize: function(name, options) {
82         OpenLayers.Layer.EventPane.prototype.initialize.apply(this, arguments);
83         OpenLayers.Layer.FixedZoomLevels.prototype.initialize.apply(this, 
84                                                                     arguments);
85         if(this.sphericalMercator) {
86             OpenLayers.Util.extend(this, OpenLayers.Layer.SphericalMercator);
87             this.initMercatorParameters();
88         }
89     },
90     
91     /**
92      * Method: loadMapObject
93      */
94     loadMapObject:function() {
95
96         // create div and set to same size as map
97         var veDiv = OpenLayers.Util.createDiv(this.name);
98         var sz = this.map.getSize();
99         veDiv.style.width = sz.w + "px";
100         veDiv.style.height = sz.h + "px";
101         this.div.appendChild(veDiv);
102
103         try { // crash prevention
104             this.mapObject = new VEMap(this.name);
105         } catch (e) { }
106
107         if (this.mapObject != null) {
108             try { // this is to catch a Mozilla bug without falling apart
109
110                 // The fourth argument is whether the map is 'fixed' -- not 
111                 // draggable. See: 
112                 // http://blogs.msdn.com/virtualearth/archive/2007/09/28/locking-a-virtual-earth-map.aspx
113                 //
114                 this.mapObject.LoadMap(null, null, this.type, true);
115                 this.mapObject.AttachEvent("onmousedown", function() {return true; });
116
117             } catch (e) { }
118             this.mapObject.HideDashboard();
119         }
120
121         //can we do smooth panning? this is an unpublished method, so we need 
122         // to be careful
123         if ( !this.mapObject ||
124              !this.mapObject.vemapcontrol ||
125              !this.mapObject.vemapcontrol.PanMap ||
126              (typeof this.mapObject.vemapcontrol.PanMap != "function")) {
127
128             this.dragPanMapObject = null;
129         }
130
131     },
132
133     /** 
134      * APIMethod: getWarningHTML
135      * 
136      * Returns: 
137      * {String} String with information on why layer is broken, how to get
138      *          it working.
139      */
140     getWarningHTML:function() {
141         return OpenLayers.i18n(
142             "getLayerWarning", {'layerType':'VE', 'layerLib':'VirtualEarth'}
143         );
144     },
145
146
147
148     /************************************
149      *                                  *
150      *   MapObject Interface Controls   *
151      *                                  *
152      ************************************/
153
154
155   // Get&Set Center, Zoom
156
157     /** 
158      * APIMethod: setMapObjectCenter
159      * Set the mapObject to the specified center and zoom
160      * 
161      * Parameters:
162      * center - {Object} MapObject LonLat format
163      * zoom - {int} MapObject zoom format
164      */
165     setMapObjectCenter: function(center, zoom) {
166         this.mapObject.SetCenterAndZoom(center, zoom); 
167     },
168    
169     /**
170      * APIMethod: getMapObjectCenter
171      * 
172      * Returns: 
173      * {Object} The mapObject's current center in Map Object format
174      */
175     getMapObjectCenter: function() {
176         return this.mapObject.GetCenter();
177     },
178
179     /**
180      * APIMethod: dragPanMapObject
181      * 
182      * Parameters:
183      * dX - {Integer}
184      * dY - {Integer}
185      */
186     dragPanMapObject: function(dX, dY) {
187         this.mapObject.vemapcontrol.PanMap(dX, -dY);
188     },
189
190     /** 
191      * APIMethod: getMapObjectZoom
192      * 
193      * Returns:
194      * {Integer} The mapObject's current zoom, in Map Object format
195      */
196     getMapObjectZoom: function() {
197         return this.mapObject.GetZoomLevel();
198     },
199
200
201   // LonLat - Pixel Translation
202   
203     /**
204      * APIMethod: getMapObjectLonLatFromMapObjectPixel
205      * 
206      * Parameters:
207      * moPixel - {Object} MapObject Pixel format
208      * 
209      * Returns:
210      * {Object} MapObject LonLat translated from MapObject Pixel
211      */
212     getMapObjectLonLatFromMapObjectPixel: function(moPixel) {
213         //the conditional here is to test if we are running the v6 of VE
214         return (typeof VEPixel != 'undefined') 
215             ? this.mapObject.PixelToLatLong(moPixel)
216             : this.mapObject.PixelToLatLong(moPixel.x, moPixel.y);
217     },
218
219     /**
220      * APIMethod: getMapObjectPixelFromMapObjectLonLat
221      * 
222      * Parameters:
223      * moLonLat - {Object} MapObject LonLat format
224      * 
225      * Returns:
226      * {Object} MapObject Pixel transtlated from MapObject LonLat
227      */
228     getMapObjectPixelFromMapObjectLonLat: function(moLonLat) {
229         return this.mapObject.LatLongToPixel(moLonLat);
230     },
231
232
233     /************************************
234      *                                  *
235      *       MapObject Primitives       *
236      *                                  *
237      ************************************/
238
239
240   // LonLat
241     
242     /**
243      * APIMethod: getLongitudeFromMapObjectLonLat
244      * 
245      * Parameters:
246      * moLonLat - {Object} MapObject LonLat format
247      * 
248      * Returns:
249      * {Float} Longitude of the given MapObject LonLat
250      */
251     getLongitudeFromMapObjectLonLat: function(moLonLat) {
252         return this.sphericalMercator ? 
253             this.forwardMercator(moLonLat.Longitude, moLonLat.Latitude).lon :
254             moLonLat.Longitude;
255     },
256
257     /**
258      * APIMethod: getLatitudeFromMapObjectLonLat
259      * 
260      * Parameters:
261      * moLonLat - {Object} MapObject LonLat format
262      * 
263      * Returns:
264      * {Float} Latitude of the given MapObject LonLat
265      */
266     getLatitudeFromMapObjectLonLat: function(moLonLat) {
267         return this.sphericalMercator ? 
268             this.forwardMercator(moLonLat.Longitude, moLonLat.Latitude).lat :
269             moLonLat.Latitude;
270     },
271
272     /**
273      * APIMethod: getMapObjectLonLatFromLonLat
274      * 
275      * Parameters:
276      * lon - {Float}
277      * lat - {Float}
278      * 
279      * Returns:
280      * {Object} MapObject LonLat built from lon and lat params
281      */
282     getMapObjectLonLatFromLonLat: function(lon, lat) {
283         var veLatLong;
284         if(this.sphericalMercator) {
285             var lonlat = this.inverseMercator(lon, lat);
286             veLatLong = new VELatLong(lonlat.lat, lonlat.lon);
287         } else {
288             veLatLong = new VELatLong(lat, lon);
289         }
290         return veLatLong;
291     },
292
293   // Pixel
294     
295     /**
296      * APIMethod: getXFromMapObjectPixel
297      * 
298      * Parameters:
299      * moPixel - {Object} MapObject Pixel format
300      * 
301      * Returns:
302      * {Integer} X value of the MapObject Pixel
303      */
304     getXFromMapObjectPixel: function(moPixel) {
305         return moPixel.x;
306     },
307
308     /**
309      * APIMethod: getYFromMapObjectPixel
310      * 
311      * Parameters:
312      * moPixel - {Object} MapObject Pixel format
313      * 
314      * Returns:
315      * {Integer} Y value of the MapObject Pixel
316      */
317     getYFromMapObjectPixel: function(moPixel) {
318         return moPixel.y;
319     },
320
321     /**
322      * APIMethod: getMapObjectPixelFromXY
323      * 
324      * Parameters:
325      * x - {Integer}
326      * y - {Integer}
327      * 
328      * Returns:
329      * {Object} MapObject Pixel from x and y parameters
330      */
331     getMapObjectPixelFromXY: function(x, y) {
332         //the conditional here is to test if we are running the v6 of VE
333         return (typeof VEPixel != 'undefined') ? new VEPixel(x, y)
334                          : new Msn.VE.Pixel(x, y);
335     },
336
337     CLASS_NAME: "OpenLayers.Layer.VirtualEarth"
338 });