Add ol.TileUrl

This commit is contained in:
Tom Payne
2012-07-05 15:03:26 +02:00
committed by Tom Payne
parent 6ced2b5815
commit c04e3d339f
3 changed files with 56 additions and 0 deletions

View File

@@ -5,3 +5,4 @@ goog.require('ol.MVCArray');
goog.require('ol.MVCObject');
goog.require('ol.TileBounds');
goog.require('ol.TileCoord');
goog.require('ol.TileUrl');

35
src/ol/tileurl.js Normal file
View File

@@ -0,0 +1,35 @@
goog.provide('ol.TileUrl');
goog.require('goog.math');
goog.require('ol.TileCoord');
/**
* @typedef {function(ol.TileCoord): string}
*/
ol.TileUrlFunction;
/**
* @param {string} template Template.
* @return {ol.TileUrlFunction} Tile URL function.
*/
ol.TileUrl.createFromTemplate = function(template) {
return function(tileCoord) {
return template.replace(/\{z\}/, tileCoord.z)
.replace(/\{x\}/, tileCoord.x)
.replace(/\{y\}/, tileCoord.y);
};
};
/**
* @param {Array.<ol.TileUrlFunction>} tileUrlFunctions Tile URL Functions.
* @return {ol.TileUrlFunction} Tile URL function.
*/
ol.TileUrl.createFromTileUrlFunctions = function(tileUrlFunctions) {
return function(tileCoord) {
var index = goog.math.modulo(tileCoord.hash(), tileUrlFunctions.length);
return tileUrlFunctions[index](tileCoord);
};
};

20
src/ol/tileurl_test.js Normal file
View File

@@ -0,0 +1,20 @@
goog.require('goog.testing.jsunit');
goog.require('ol.TileCoord');
goog.require('ol.TileUrl');
function testCreateFromTemplate() {
var tileUrl = ol.TileUrl.createFromTemplate('{z}/{x}/{y}');
assertEquals('3/2/1', tileUrl(new ol.TileCoord(3, 2, 1)));
}
function testCreateFromTileUrlFunctions() {
var tileUrl = ol.TileUrl.createFromTileUrlFunctions([
ol.TileUrl.createFromTemplate('a'),
ol.TileUrl.createFromTemplate('b')
]);
var tileUrl1 = tileUrl(new ol.TileCoord(1, 0, 0));
var tileUrl2 = tileUrl(new ol.TileCoord(1, 0, 1));
assertTrue(tileUrl1 != tileUrl2);
}