[ol.layer.TileLayer] add a getTileForXYZ func

This commit is contained in:
Éric Lemoine
2012-06-22 15:16:46 +02:00
parent 976239816e
commit 71a804c5e0
3 changed files with 51 additions and 10 deletions

View File

@@ -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 */

View File

@@ -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

View File

@@ -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);
});
});
});