Yahoo.js 12 KB

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