VirtualEarth.js 11 KB

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