EventPane.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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.js
  7. * @requires OpenLayers/Util.js
  8. */
  9. /**
  10. * Class: OpenLayers.Layer.EventPane
  11. * Base class for 3rd party layers. Create a new event pane layer with the
  12. * <OpenLayers.Layer.EventPane> constructor.
  13. *
  14. * Inherits from:
  15. * - <OpenLayers.Layer>
  16. */
  17. OpenLayers.Layer.EventPane = OpenLayers.Class(OpenLayers.Layer, {
  18. /**
  19. * APIProperty: smoothDragPan
  20. * {Boolean} smoothDragPan determines whether non-public/internal API
  21. * methods are used for better performance while dragging EventPane
  22. * layers. When not in sphericalMercator mode, the smoother dragging
  23. * doesn't actually move north/south directly with the number of
  24. * pixels moved, resulting in a slight offset when you drag your mouse
  25. * north south with this option on. If this visual disparity bothers
  26. * you, you should turn this option off, or use spherical mercator.
  27. * Default is on.
  28. */
  29. smoothDragPan: true,
  30. /**
  31. * Property: isBaseLayer
  32. * {Boolean} EventPaned layers are always base layers, by necessity.
  33. */
  34. isBaseLayer: true,
  35. /**
  36. * APIProperty: isFixed
  37. * {Boolean} EventPaned layers are fixed by default.
  38. */
  39. isFixed: true,
  40. /**
  41. * Property: pane
  42. * {DOMElement} A reference to the element that controls the events.
  43. */
  44. pane: null,
  45. /**
  46. * Property: mapObject
  47. * {Object} This is the object which will be used to load the 3rd party library
  48. * in the case of the google layer, this will be of type GMap,
  49. * in the case of the ve layer, this will be of type VEMap
  50. */
  51. mapObject: null,
  52. /**
  53. * Constructor: OpenLayers.Layer.EventPane
  54. * Create a new event pane layer
  55. *
  56. * Parameters:
  57. * name - {String}
  58. * options - {Object} Hashtable of extra options to tag onto the layer
  59. */
  60. initialize: function(name, options) {
  61. OpenLayers.Layer.prototype.initialize.apply(this, arguments);
  62. if (this.pane == null) {
  63. this.pane = OpenLayers.Util.createDiv(this.div.id + "_EventPane");
  64. }
  65. },
  66. /**
  67. * APIMethod: destroy
  68. * Deconstruct this layer.
  69. */
  70. destroy: function() {
  71. this.mapObject = null;
  72. this.pane = null;
  73. OpenLayers.Layer.prototype.destroy.apply(this, arguments);
  74. },
  75. /**
  76. * Method: setMap
  77. * Set the map property for the layer. This is done through an accessor
  78. * so that subclasses can override this and take special action once
  79. * they have their map variable set.
  80. *
  81. * Parameters:
  82. * map - {<OpenLayers.Map>}
  83. */
  84. setMap: function(map) {
  85. OpenLayers.Layer.prototype.setMap.apply(this, arguments);
  86. this.pane.style.zIndex = parseInt(this.div.style.zIndex) + 1;
  87. this.pane.style.display = this.div.style.display;
  88. this.pane.style.width="100%";
  89. this.pane.style.height="100%";
  90. if (OpenLayers.BROWSER_NAME == "msie") {
  91. this.pane.style.background =
  92. "url(" + OpenLayers.Util.getImagesLocation() + "blank.gif)";
  93. }
  94. if (this.isFixed) {
  95. this.map.eventsDiv.appendChild(this.pane);
  96. } else {
  97. this.map.layerContainerDiv.appendChild(this.pane);
  98. }
  99. // once our layer has been added to the map, we can load it
  100. this.loadMapObject();
  101. // if map didn't load, display warning
  102. if (this.mapObject == null) {
  103. this.loadWarningMessage();
  104. }
  105. },
  106. /**
  107. * APIMethod: removeMap
  108. * On being removed from the map, we'll like to remove the invisible 'pane'
  109. * div that we added to it on creation.
  110. *
  111. * Parameters:
  112. * map - {<OpenLayers.Map>}
  113. */
  114. removeMap: function(map) {
  115. if (this.pane && this.pane.parentNode) {
  116. this.pane.parentNode.removeChild(this.pane);
  117. }
  118. OpenLayers.Layer.prototype.removeMap.apply(this, arguments);
  119. },
  120. /**
  121. * Method: loadWarningMessage
  122. * If we can't load the map lib, then display an error message to the
  123. * user and tell them where to go for help.
  124. *
  125. * This function sets up the layout for the warning message. Each 3rd
  126. * party layer must implement its own getWarningHTML() function to
  127. * provide the actual warning message.
  128. */
  129. loadWarningMessage:function() {
  130. this.div.style.backgroundColor = "darkblue";
  131. var viewSize = this.map.getSize();
  132. var msgW = Math.min(viewSize.w, 300);
  133. var msgH = Math.min(viewSize.h, 200);
  134. var size = new OpenLayers.Size(msgW, msgH);
  135. var centerPx = new OpenLayers.Pixel(viewSize.w/2, viewSize.h/2);
  136. var topLeft = centerPx.add(-size.w/2, -size.h/2);
  137. var div = OpenLayers.Util.createDiv(this.name + "_warning",
  138. topLeft,
  139. size,
  140. null,
  141. null,
  142. null,
  143. "auto");
  144. div.style.padding = "7px";
  145. div.style.backgroundColor = "yellow";
  146. div.innerHTML = this.getWarningHTML();
  147. this.div.appendChild(div);
  148. },
  149. /**
  150. * Method: getWarningHTML
  151. * To be implemented by subclasses.
  152. *
  153. * Returns:
  154. * {String} String with information on why layer is broken, how to get
  155. * it working.
  156. */
  157. getWarningHTML:function() {
  158. //should be implemented by subclasses
  159. return "";
  160. },
  161. /**
  162. * Method: display
  163. * Set the display on the pane
  164. *
  165. * Parameters:
  166. * display - {Boolean}
  167. */
  168. display: function(display) {
  169. OpenLayers.Layer.prototype.display.apply(this, arguments);
  170. this.pane.style.display = this.div.style.display;
  171. },
  172. /**
  173. * Method: setZIndex
  174. * Set the z-index order for the pane.
  175. *
  176. * Parameters:
  177. * zIndex - {int}
  178. */
  179. setZIndex: function (zIndex) {
  180. OpenLayers.Layer.prototype.setZIndex.apply(this, arguments);
  181. this.pane.style.zIndex = parseInt(this.div.style.zIndex) + 1;
  182. },
  183. /**
  184. * Method: moveByPx
  185. * Move the layer based on pixel vector. To be implemented by subclasses.
  186. *
  187. * Parameters:
  188. * dx - {Number} The x coord of the displacement vector.
  189. * dy - {Number} The y coord of the displacement vector.
  190. */
  191. moveByPx: function(dx, dy) {
  192. OpenLayers.Layer.prototype.moveByPx.apply(this, arguments);
  193. if (this.dragPanMapObject) {
  194. this.dragPanMapObject(dx, -dy);
  195. } else {
  196. this.moveTo(this.map.getCachedCenter());
  197. }
  198. },
  199. /**
  200. * Method: moveTo
  201. * Handle calls to move the layer.
  202. *
  203. * Parameters:
  204. * bounds - {<OpenLayers.Bounds>}
  205. * zoomChanged - {Boolean}
  206. * dragging - {Boolean}
  207. */
  208. moveTo:function(bounds, zoomChanged, dragging) {
  209. OpenLayers.Layer.prototype.moveTo.apply(this, arguments);
  210. if (this.mapObject != null) {
  211. var newCenter = this.map.getCenter();
  212. var newZoom = this.map.getZoom();
  213. if (newCenter != null) {
  214. var moOldCenter = this.getMapObjectCenter();
  215. var oldCenter = this.getOLLonLatFromMapObjectLonLat(moOldCenter);
  216. var moOldZoom = this.getMapObjectZoom();
  217. var oldZoom= this.getOLZoomFromMapObjectZoom(moOldZoom);
  218. if ( !(newCenter.equals(oldCenter)) ||
  219. !(newZoom == oldZoom) ) {
  220. if (!zoomChanged && oldCenter && this.dragPanMapObject &&
  221. this.smoothDragPan) {
  222. var oldPx = this.map.getViewPortPxFromLonLat(oldCenter);
  223. var newPx = this.map.getViewPortPxFromLonLat(newCenter);
  224. this.dragPanMapObject(newPx.x-oldPx.x, oldPx.y-newPx.y);
  225. } else {
  226. var center = this.getMapObjectLonLatFromOLLonLat(newCenter);
  227. var zoom = this.getMapObjectZoomFromOLZoom(newZoom);
  228. this.setMapObjectCenter(center, zoom, dragging);
  229. }
  230. }
  231. }
  232. }
  233. },
  234. /********************************************************/
  235. /* */
  236. /* Baselayer Functions */
  237. /* */
  238. /********************************************************/
  239. /**
  240. * Method: getLonLatFromViewPortPx
  241. * Get a map location from a pixel location
  242. *
  243. * Parameters:
  244. * viewPortPx - {<OpenLayers.Pixel>}
  245. *
  246. * Returns:
  247. * {<OpenLayers.LonLat>} An OpenLayers.LonLat which is the passed-in view
  248. * port OpenLayers.Pixel, translated into lon/lat by map lib
  249. * If the map lib is not loaded or not centered, returns null
  250. */
  251. getLonLatFromViewPortPx: function (viewPortPx) {
  252. var lonlat = null;
  253. if ( (this.mapObject != null) &&
  254. (this.getMapObjectCenter() != null) ) {
  255. var moPixel = this.getMapObjectPixelFromOLPixel(viewPortPx);
  256. var moLonLat = this.getMapObjectLonLatFromMapObjectPixel(moPixel);
  257. lonlat = this.getOLLonLatFromMapObjectLonLat(moLonLat);
  258. }
  259. return lonlat;
  260. },
  261. /**
  262. * Method: getViewPortPxFromLonLat
  263. * Get a pixel location from a map location
  264. *
  265. * Parameters:
  266. * lonlat - {<OpenLayers.LonLat>}
  267. *
  268. * Returns:
  269. * {<OpenLayers.Pixel>} An OpenLayers.Pixel which is the passed-in
  270. * OpenLayers.LonLat, translated into view port pixels by map lib
  271. * If map lib is not loaded or not centered, returns null
  272. */
  273. getViewPortPxFromLonLat: function (lonlat) {
  274. var viewPortPx = null;
  275. if ( (this.mapObject != null) &&
  276. (this.getMapObjectCenter() != null) ) {
  277. var moLonLat = this.getMapObjectLonLatFromOLLonLat(lonlat);
  278. var moPixel = this.getMapObjectPixelFromMapObjectLonLat(moLonLat);
  279. viewPortPx = this.getOLPixelFromMapObjectPixel(moPixel);
  280. }
  281. return viewPortPx;
  282. },
  283. /********************************************************/
  284. /* */
  285. /* Translation Functions */
  286. /* */
  287. /* The following functions translate Map Object and */
  288. /* OL formats for Pixel, LonLat */
  289. /* */
  290. /********************************************************/
  291. //
  292. // TRANSLATION: MapObject LatLng <-> OpenLayers.LonLat
  293. //
  294. /**
  295. * Method: getOLLonLatFromMapObjectLonLat
  296. * Get an OL style map location from a 3rd party style map location
  297. *
  298. * Parameters
  299. * moLonLat - {Object}
  300. *
  301. * Returns:
  302. * {<OpenLayers.LonLat>} An OpenLayers.LonLat, translated from the passed in
  303. * MapObject LonLat
  304. * Returns null if null value is passed in
  305. */
  306. getOLLonLatFromMapObjectLonLat: function(moLonLat) {
  307. var olLonLat = null;
  308. if (moLonLat != null) {
  309. var lon = this.getLongitudeFromMapObjectLonLat(moLonLat);
  310. var lat = this.getLatitudeFromMapObjectLonLat(moLonLat);
  311. olLonLat = new OpenLayers.LonLat(lon, lat);
  312. }
  313. return olLonLat;
  314. },
  315. /**
  316. * Method: getMapObjectLonLatFromOLLonLat
  317. * Get a 3rd party map location from an OL map location.
  318. *
  319. * Parameters:
  320. * olLonLat - {<OpenLayers.LonLat>}
  321. *
  322. * Returns:
  323. * {Object} A MapObject LonLat, translated from the passed in
  324. * OpenLayers.LonLat
  325. * Returns null if null value is passed in
  326. */
  327. getMapObjectLonLatFromOLLonLat: function(olLonLat) {
  328. var moLatLng = null;
  329. if (olLonLat != null) {
  330. moLatLng = this.getMapObjectLonLatFromLonLat(olLonLat.lon,
  331. olLonLat.lat);
  332. }
  333. return moLatLng;
  334. },
  335. //
  336. // TRANSLATION: MapObject Pixel <-> OpenLayers.Pixel
  337. //
  338. /**
  339. * Method: getOLPixelFromMapObjectPixel
  340. * Get an OL pixel location from a 3rd party pixel location.
  341. *
  342. * Parameters:
  343. * moPixel - {Object}
  344. *
  345. * Returns:
  346. * {<OpenLayers.Pixel>} An OpenLayers.Pixel, translated from the passed in
  347. * MapObject Pixel
  348. * Returns null if null value is passed in
  349. */
  350. getOLPixelFromMapObjectPixel: function(moPixel) {
  351. var olPixel = null;
  352. if (moPixel != null) {
  353. var x = this.getXFromMapObjectPixel(moPixel);
  354. var y = this.getYFromMapObjectPixel(moPixel);
  355. olPixel = new OpenLayers.Pixel(x, y);
  356. }
  357. return olPixel;
  358. },
  359. /**
  360. * Method: getMapObjectPixelFromOLPixel
  361. * Get a 3rd party pixel location from an OL pixel location
  362. *
  363. * Parameters:
  364. * olPixel - {<OpenLayers.Pixel>}
  365. *
  366. * Returns:
  367. * {Object} A MapObject Pixel, translated from the passed in
  368. * OpenLayers.Pixel
  369. * Returns null if null value is passed in
  370. */
  371. getMapObjectPixelFromOLPixel: function(olPixel) {
  372. var moPixel = null;
  373. if (olPixel != null) {
  374. moPixel = this.getMapObjectPixelFromXY(olPixel.x, olPixel.y);
  375. }
  376. return moPixel;
  377. },
  378. CLASS_NAME: "OpenLayers.Layer.EventPane"
  379. });