From 81d5849207df46b1674a5142b40bb786a3ac7611 Mon Sep 17 00:00:00 2001 From: Tim Schaub Date: Wed, 18 May 2016 18:27:19 -0600 Subject: [PATCH] Add a settable key for all tile sources The source's key is used as the key for all tiles in the source. For URL tile sources, the URL is the key. --- src/ol/source/tilesource.js | 21 ++++++++++++++++++++- src/ol/source/urltilesource.js | 18 ++++++++++-------- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/src/ol/source/tilesource.js b/src/ol/source/tilesource.js index bc0b789691..14542a41df 100644 --- a/src/ol/source/tilesource.js +++ b/src/ol/source/tilesource.js @@ -67,6 +67,12 @@ ol.source.Tile = function(options) { */ this.tmpSize = [0, 0]; + /** + * @private + * @type {string} + */ + this.key_ = ''; + }; goog.inherits(ol.source.Tile, ol.source.Source); @@ -144,7 +150,20 @@ ol.source.Tile.prototype.getGutter = function(projection) { * @protected */ ol.source.Tile.prototype.getKeyParams = function() { - return ''; + return this.key_; +}; + + +/** + * Set the value to be used as the key for all tiles in the source. + * @param {string} key The key for tiles. + * @protected + */ +ol.source.Tile.prototype.setKey = function(key) { + if (this.key_ !== key) { + this.key_ = key; + this.changed(); + } }; diff --git a/src/ol/source/urltilesource.js b/src/ol/source/urltilesource.js index 73a10b6a24..fd359b1c29 100644 --- a/src/ol/source/urltilesource.js +++ b/src/ol/source/urltilesource.js @@ -143,15 +143,16 @@ ol.source.UrlTile.prototype.setTileLoadFunction = function(tileLoadFunction) { /** * Set the tile URL function of the source. * @param {ol.TileUrlFunctionType} tileUrlFunction Tile URL function. + * @param {string=} opt_key Optional new tile key for the source. * @api */ -ol.source.UrlTile.prototype.setTileUrlFunction = function(tileUrlFunction) { - // FIXME It should be possible to be more intelligent and avoid clearing the - // FIXME cache. The tile URL function would need to be incorporated into the - // FIXME cache key somehow. - this.tileCache.clear(); +ol.source.UrlTile.prototype.setTileUrlFunction = function(tileUrlFunction, opt_key) { this.tileUrlFunction = tileUrlFunction; - this.changed(); + if (typeof opt_key !== 'undefined') { + this.setKey(opt_key); + } else { + this.changed(); + } }; @@ -164,7 +165,7 @@ ol.source.UrlTile.prototype.setUrl = function(url) { var urls = this.urls = ol.TileUrlFunction.expandUrl(url); this.setTileUrlFunction(this.fixedTileUrlFunction ? this.fixedTileUrlFunction.bind(this) : - ol.TileUrlFunction.createFromTemplates(urls, this.tileGrid)); + ol.TileUrlFunction.createFromTemplates(urls, this.tileGrid), url); }; @@ -175,9 +176,10 @@ ol.source.UrlTile.prototype.setUrl = function(url) { */ ol.source.UrlTile.prototype.setUrls = function(urls) { this.urls = urls; + var key = urls.join('\n'); this.setTileUrlFunction(this.fixedTileUrlFunction ? this.fixedTileUrlFunction.bind(this) : - ol.TileUrlFunction.createFromTemplates(urls, this.tileGrid)); + ol.TileUrlFunction.createFromTemplates(urls, this.tileGrid), key); };