1
/* Copyright (c) 2006-2008 MetaCarta, Inc., published under the Clear BSD
2
* license. See http://svn.openlayers.org/trunk/openlayers/license.txt for the
3
* full text of the license. */
7
* @requires OpenLayers/Util.js
11
* Class: OpenLayers.Tile
12
* This is a class designed to designate a single tile, however
13
* it is explicitly designed to do relatively little. Tiles store
14
* information about themselves -- such as the URL that they are related
15
* to, and their size - but do not add themselves to the layer div
16
* automatically, for example. Create a new tile with the
17
* <OpenLayers.Tile> constructor, or a subclass.
19
* TBD 3.0 - remove reference to url in above paragraph
22
OpenLayers.Tile = OpenLayers.Class({
25
* Constant: EVENT_TYPES
26
* {Array(String)} Supported application event types
28
EVENT_TYPES: [ "loadstart", "loadend", "reload", "unload"],
32
* {<OpenLayers.Events>} An events object that handles all
45
* {<OpenLayers.Layer>} layer the tile is attached to
51
* {String} url of the request.
54
* Deprecated. The base tile class does not need an url. This should be
55
* handled in subclasses. Does not belong here.
61
* {<OpenLayers.Bounds>} null
67
* {<OpenLayers.Size>} null
73
* {<OpenLayers.Pixel>} Top Left pixel of the tile
79
* {Boolean} Is the tile loading?
83
/** TBD 3.0 -- remove 'url' from the list of parameters to the constructor.
84
* there is no need for the base tile class to have a url.
86
* Constructor: OpenLayers.Tile
87
* Constructor for a new <OpenLayers.Tile> instance.
90
* layer - {<OpenLayers.Layer>} layer that the tile will go in.
91
* position - {<OpenLayers.Pixel>}
92
* bounds - {<OpenLayers.Bounds>}
94
* size - {<OpenLayers.Size>}
96
initialize: function(layer, position, bounds, url, size) {
98
this.position = position.clone();
99
this.bounds = bounds.clone();
101
this.size = size.clone();
103
//give the tile a unique id based on its BBOX.
104
this.id = OpenLayers.Util.createUniqueID("Tile_");
106
this.events = new OpenLayers.Events(this, null, this.EVENT_TYPES);
111
* Call immediately before destroying if you are listening to tile
112
* events, so that counters are properly handled if tile is still
113
* loading at destroy-time. Will only fire an event if the tile is
117
if (this.isLoading) {
118
this.isLoading = false;
119
this.events.triggerEvent("unload");
125
* Nullify references to prevent circular references and memory leaks.
131
this.position = null;
133
this.events.destroy();
141
* obj - {<OpenLayers.Tile>} The tile to be cloned
144
* {<OpenLayers.Tile>} An exact clone of this <OpenLayers.Tile>
146
clone: function (obj) {
148
obj = new OpenLayers.Tile(this.layer,
155
// catch any randomly tagged-on properties
156
OpenLayers.Util.applyDefaults(obj, this);
163
* Clear whatever is currently in the tile, then return whether or not
164
* it should actually be re-drawn.
167
* {Boolean} Whether or not the tile should actually be drawn. Note that
168
* this is not really the best way of doing things, but such is
169
* the way the code has been developed. Subclasses call this and
170
* depend on the return to know if they should draw or not.
173
var maxExtent = this.layer.maxExtent;
174
var withinMaxExtent = (maxExtent &&
175
this.bounds.intersectsBounds(maxExtent, false));
177
// The only case where we *wouldn't* want to draw the tile is if the
178
// tile is outside its layer's maxExtent.
179
this.shouldDraw = (withinMaxExtent || this.layer.displayOutsideMaxExtent);
181
//clear tile's contents and mark as not drawn
184
return this.shouldDraw;
189
* Reposition the tile.
192
* bounds - {<OpenLayers.Bounds>}
193
* position - {<OpenLayers.Pixel>}
194
* redraw - {Boolean} Call draw method on tile after moving.
197
moveTo: function (bounds, position, redraw) {
198
if (redraw == null) {
202
this.bounds = bounds.clone();
203
this.position = position.clone();
211
* Clear the tile of any bounds/position-related data so that it can
212
* be reused in a new location. To be implemented by subclasses.
215
// to be implemented by subclasses
219
* Method: getBoundsFromBaseLayer
220
* Take the pixel locations of the corner of the tile, and pass them to
221
* the base layer and ask for the location of those pixels, so that
222
* displaying tiles over Google works fine.
225
* position - {<OpenLayers.Pixel>}
228
* bounds - {<OpenLayers.Bounds>}
230
getBoundsFromBaseLayer: function(position) {
231
var msg = OpenLayers.i18n('reprojectDeprecated',
232
{'layerName':this.layer.name});
233
OpenLayers.Console.warn(msg);
234
var topLeft = this.layer.map.getLonLatFromLayerPx(position);
235
var bottomRightPx = position.clone();
236
bottomRightPx.x += this.size.w;
237
bottomRightPx.y += this.size.h;
238
var bottomRight = this.layer.map.getLonLatFromLayerPx(bottomRightPx);
239
// Handle the case where the base layer wraps around the date line.
240
// Google does this, and it breaks WMS servers to request bounds in
242
if (topLeft.lon > bottomRight.lon) {
243
if (topLeft.lon < 0) {
244
topLeft.lon = -180 - (topLeft.lon+180);
246
bottomRight.lon = 180+bottomRight.lon+180;
249
var bounds = new OpenLayers.Bounds(topLeft.lon,
258
* Show the tile only if it should be drawn.
260
showTile: function() {
261
if (this.shouldDraw) {
268
* Show the tile. To be implemented by subclasses.
270
show: function() { },
274
* Hide the tile. To be implemented by subclasses.
276
hide: function() { },
278
CLASS_NAME: "OpenLayers.Tile"