EventPane.js 14 KB

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