Tile.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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/BaseTypes/Class.js
  7. * @requires OpenLayers/Util.js
  8. */
  9. /**
  10. * Class: OpenLayers.Tile
  11. * This is a class designed to designate a single tile, however
  12. * it is explicitly designed to do relatively little. Tiles store
  13. * information about themselves -- such as the URL that they are related
  14. * to, and their size - but do not add themselves to the layer div
  15. * automatically, for example. Create a new tile with the
  16. * <OpenLayers.Tile> constructor, or a subclass.
  17. *
  18. * TBD 3.0 - remove reference to url in above paragraph
  19. *
  20. */
  21. OpenLayers.Tile = OpenLayers.Class({
  22. /**
  23. * APIProperty: events
  24. * {<OpenLayers.Events>} An events object that handles all
  25. * events on the tile.
  26. *
  27. * Register a listener for a particular event with the following syntax:
  28. * (code)
  29. * tile.events.register(type, obj, listener);
  30. * (end)
  31. *
  32. * Supported event types:
  33. * beforedraw - Triggered before the tile is drawn. Used to defer
  34. * drawing to an animation queue. To defer drawing, listeners need
  35. * to return false, which will abort drawing. The queue handler needs
  36. * to call <draw>(true) to actually draw the tile.
  37. * loadstart - Triggered when tile loading starts.
  38. * loadend - Triggered when tile loading ends.
  39. * loaderror - Triggered before the loadend event (i.e. when the tile is
  40. * still hidden) if the tile could not be loaded.
  41. * reload - Triggered when an already loading tile is reloaded.
  42. * unload - Triggered before a tile is unloaded.
  43. */
  44. events: null,
  45. /**
  46. * APIProperty: eventListeners
  47. * {Object} If set as an option at construction, the eventListeners
  48. * object will be registered with <OpenLayers.Events.on>. Object
  49. * structure must be a listeners object as shown in the example for
  50. * the events.on method.
  51. *
  52. * This options can be set in the ``tileOptions`` option from
  53. * <OpenLayers.Layer.Grid>. For example, to be notified of the
  54. * ``loadend`` event of each tiles:
  55. * (code)
  56. * new OpenLayers.Layer.OSM('osm', 'http://tile.openstreetmap.org/${z}/${x}/${y}.png', {
  57. * tileOptions: {
  58. * eventListeners: {
  59. * 'loadend': function(evt) {
  60. * // do something on loadend
  61. * }
  62. * }
  63. * }
  64. * });
  65. * (end)
  66. */
  67. eventListeners: null,
  68. /**
  69. * Property: id
  70. * {String} null
  71. */
  72. id: null,
  73. /**
  74. * Property: layer
  75. * {<OpenLayers.Layer>} layer the tile is attached to
  76. */
  77. layer: null,
  78. /**
  79. * Property: url
  80. * {String} url of the request.
  81. *
  82. * TBD 3.0
  83. * Deprecated. The base tile class does not need an url. This should be
  84. * handled in subclasses. Does not belong here.
  85. */
  86. url: null,
  87. /**
  88. * APIProperty: bounds
  89. * {<OpenLayers.Bounds>} null
  90. */
  91. bounds: null,
  92. /**
  93. * Property: size
  94. * {<OpenLayers.Size>} null
  95. */
  96. size: null,
  97. /**
  98. * Property: position
  99. * {<OpenLayers.Pixel>} Top Left pixel of the tile
  100. */
  101. position: null,
  102. /**
  103. * Property: isLoading
  104. * {Boolean} Is the tile loading?
  105. */
  106. isLoading: false,
  107. /** TBD 3.0 -- remove 'url' from the list of parameters to the constructor.
  108. * there is no need for the base tile class to have a url.
  109. */
  110. /**
  111. * Constructor: OpenLayers.Tile
  112. * Constructor for a new <OpenLayers.Tile> instance.
  113. *
  114. * Parameters:
  115. * layer - {<OpenLayers.Layer>} layer that the tile will go in.
  116. * position - {<OpenLayers.Pixel>}
  117. * bounds - {<OpenLayers.Bounds>}
  118. * url - {<String>}
  119. * size - {<OpenLayers.Size>}
  120. * options - {Object}
  121. */
  122. initialize: function(layer, position, bounds, url, size, options) {
  123. this.layer = layer;
  124. this.position = position.clone();
  125. this.setBounds(bounds);
  126. this.url = url;
  127. if (size) {
  128. this.size = size.clone();
  129. }
  130. //give the tile a unique id based on its BBOX.
  131. this.id = OpenLayers.Util.createUniqueID("Tile_");
  132. OpenLayers.Util.extend(this, options);
  133. this.events = new OpenLayers.Events(this);
  134. if (this.eventListeners instanceof Object) {
  135. this.events.on(this.eventListeners);
  136. }
  137. },
  138. /**
  139. * Method: unload
  140. * Call immediately before destroying if you are listening to tile
  141. * events, so that counters are properly handled if tile is still
  142. * loading at destroy-time. Will only fire an event if the tile is
  143. * still loading.
  144. */
  145. unload: function() {
  146. if (this.isLoading) {
  147. this.isLoading = false;
  148. this.events.triggerEvent("unload");
  149. }
  150. },
  151. /**
  152. * APIMethod: destroy
  153. * Nullify references to prevent circular references and memory leaks.
  154. */
  155. destroy:function() {
  156. this.layer = null;
  157. this.bounds = null;
  158. this.size = null;
  159. this.position = null;
  160. if (this.eventListeners) {
  161. this.events.un(this.eventListeners);
  162. }
  163. this.events.destroy();
  164. this.eventListeners = null;
  165. this.events = null;
  166. },
  167. /**
  168. * Method: draw
  169. * Clear whatever is currently in the tile, then return whether or not
  170. * it should actually be re-drawn. This is an example implementation
  171. * that can be overridden by subclasses. The minimum thing to do here
  172. * is to call <clear> and return the result from <shouldDraw>.
  173. *
  174. * Parameters:
  175. * force - {Boolean} If true, the tile will not be cleared and no beforedraw
  176. * event will be fired. This is used for drawing tiles asynchronously
  177. * after drawing has been cancelled by returning false from a beforedraw
  178. * listener.
  179. *
  180. * Returns:
  181. * {Boolean} Whether or not the tile should actually be drawn. Returns null
  182. * if a beforedraw listener returned false.
  183. */
  184. draw: function(force) {
  185. if (!force) {
  186. //clear tile's contents and mark as not drawn
  187. this.clear();
  188. }
  189. var draw = this.shouldDraw();
  190. if (draw && !force && this.events.triggerEvent("beforedraw") === false) {
  191. draw = null;
  192. }
  193. return draw;
  194. },
  195. /**
  196. * Method: shouldDraw
  197. * Return whether or not the tile should actually be (re-)drawn. The only
  198. * case where we *wouldn't* want to draw the tile is if the tile is outside
  199. * its layer's maxExtent
  200. *
  201. * Returns:
  202. * {Boolean} Whether or not the tile should actually be drawn.
  203. */
  204. shouldDraw: function() {
  205. var withinMaxExtent = false,
  206. maxExtent = this.layer.maxExtent;
  207. if (maxExtent) {
  208. var map = this.layer.map;
  209. var worldBounds = map.baseLayer.wrapDateLine && map.getMaxExtent();
  210. if (this.bounds.intersectsBounds(maxExtent, {inclusive: false, worldBounds: worldBounds})) {
  211. withinMaxExtent = true;
  212. }
  213. }
  214. return withinMaxExtent || this.layer.displayOutsideMaxExtent;
  215. },
  216. /**
  217. * Method: setBounds
  218. * Sets the bounds on this instance
  219. *
  220. * Parameters:
  221. * bounds {<OpenLayers.Bounds>}
  222. */
  223. setBounds: function(bounds) {
  224. bounds = bounds.clone();
  225. if (this.layer.map.baseLayer.wrapDateLine) {
  226. var worldExtent = this.layer.map.getMaxExtent(),
  227. tolerance = this.layer.map.getResolution();
  228. bounds = bounds.wrapDateLine(worldExtent, {
  229. leftTolerance: tolerance,
  230. rightTolerance: tolerance
  231. });
  232. }
  233. this.bounds = bounds;
  234. },
  235. /**
  236. * Method: moveTo
  237. * Reposition the tile.
  238. *
  239. * Parameters:
  240. * bounds - {<OpenLayers.Bounds>}
  241. * position - {<OpenLayers.Pixel>}
  242. * redraw - {Boolean} Call draw method on tile after moving.
  243. * Default is true
  244. */
  245. moveTo: function (bounds, position, redraw) {
  246. if (redraw == null) {
  247. redraw = true;
  248. }
  249. this.setBounds(bounds);
  250. this.position = position.clone();
  251. if (redraw) {
  252. this.draw();
  253. }
  254. },
  255. /**
  256. * Method: clear
  257. * Clear the tile of any bounds/position-related data so that it can
  258. * be reused in a new location.
  259. */
  260. clear: function(draw) {
  261. // to be extended by subclasses
  262. },
  263. CLASS_NAME: "OpenLayers.Tile"
  264. });