diff --git a/src/ol/Tile.js b/src/ol/Tile.js index 838c592c2d..fe3b715d47 100644 --- a/src/ol/Tile.js +++ b/src/ol/Tile.js @@ -9,9 +9,9 @@ goog.require('ol.event.Events'); * The Tile class. * @constructor * @param {string} url - * @param {ol.Bounds} bounds + * @param {ol.Bounds|undefined} opt_bounds */ -ol.Tile = function(url, bounds) { +ol.Tile = function(url, opt_bounds) { /** * @private @@ -21,9 +21,9 @@ ol.Tile = function(url, bounds) { /** * @private - * @type {ol.Bounds} + * @type {ol.Bounds|undefined} */ - this.bounds_ = bounds; + this.bounds_ = opt_bounds; /** * @private @@ -66,7 +66,7 @@ ol.Tile.prototype.createImage = function() { * Load the tile. A tile should loaded only once. */ ol.Tile.prototype.load = function() { - goog.asserts.assert(!this.loaded && this.loading_); + goog.asserts.assert(!this.loaded && !this.loading_); this.loading_ = true; this.img_.src = this.url_; }; @@ -81,7 +81,7 @@ ol.Tile.prototype.getUrl = function() { /** * Get the tile bounds. - * @return {ol.Bounds} + * @return {ol.Bounds|undefined} */ ol.Tile.prototype.getBounds = function() { return this.bounds_; @@ -155,15 +155,15 @@ ol.Tile.createImage = (function() { * for the tiles. * @param {number} width * @param {number} height - * @return {function(new:ol.Tile, string, ol.Bounds)} + * @return {function(new:ol.Tile, string, ol.Bounds=)} */ ol.Tile.createConstructor = function(width, height) { /** * @constructor * @extends {ol.Tile} */ - var Tile = function(url, bounds) { - goog.base(this, url, bounds); + var Tile = function(url, opt_bounds) { + goog.base(this, url, opt_bounds); }; goog.inherits(Tile, ol.Tile); /** @inheritDoc */ diff --git a/src/ol/layer/TileLayer.js b/src/ol/layer/TileLayer.js index 29164f7960..146e56b1b4 100644 --- a/src/ol/layer/TileLayer.js +++ b/src/ol/layer/TileLayer.js @@ -42,7 +42,7 @@ ol.layer.TileLayer = function() { /** * @protected - * @type {function(new:ol.Tile, string, ol.Bounds)} + * @type {function(new:ol.Tile, string, ol.Bounds=)} */ this.Tile = ol.Tile.createConstructor(this.tileWidth_, this.tileHeight_); @@ -285,6 +285,25 @@ ol.layer.TileLayer.prototype.getTile = function(url, bounds) { return tile; }; +/** + * Get a tile from the cache, or create a tile and add to + * the cache. + * @param {number} x + * @param {number} y + * @param {number} z + */ +ol.layer.TileLayer.prototype.getTileForXYZ = function(x, y, z) { + var url = this.url_.replace('{x}', x + '') + .replace('{y}', y + '') + .replace('{z}', z + ''); + var tile = this.cache_.get(url); + if (!goog.isDef(tile)) { + tile = new this.Tile(url); + this.cache_.set(tile.getUrl(), tile); + } + return tile; +}; + /** * Get data from the layer. This is the layer's main API function. * @param {ol.Bounds} bounds diff --git a/test/spec/ol/layer/TileLayer.test.js b/test/spec/ol/layer/TileLayer.test.js index f34a1794f8..275b273480 100644 --- a/test/spec/ol/layer/TileLayer.test.js +++ b/test/spec/ol/layer/TileLayer.test.js @@ -329,4 +329,26 @@ describe('ol.layer.TileLayer', function() { }); }); + + describe('get a tile', function() { + var layer; + + beforeEach(function() { + layer = new ol.layer.TileLayer(); + layer.setUrl('/{z}/{x}/{y}'); + layer.setResolutions([1, 0.5, 0.25]); + layer.setTileOrigin(-128, 128); + }); + + it('returns the expected tile', function() { + var tile = layer.getTileForXYZ(1, 2, 2); + expect(tile.getUrl()).toEqual('/2/1/2'); + //var bounds = tile.getBounds(); + //expect(bounds.getMinX()).toEqual(-64); + //expect(bounds.getMinY()).toEqual(0); + //expect(bounds.getMaxX()).toEqual(0); + //expect(bounds.getMaxY()).toEqual(64); + }); + + }); });