Add convenient syntax for specifying multiple tile servers

This commit is contained in:
Tom Payne
2012-08-06 15:50:12 +02:00
parent 4a76efbbc7
commit 3cb1d9f64f

View File

@@ -16,15 +16,28 @@ ol.TileUrlFunctionType;
* @return {ol.TileUrlFunctionType} Tile URL function.
*/
ol.TileUrlFunction.createFromTemplate = function(template) {
return function(tileCoord) {
if (goog.isNull(tileCoord)) {
return undefined;
} else {
return template.replace('{z}', tileCoord.z)
.replace('{x}', tileCoord.x)
.replace('{y}', tileCoord.y);
var match =
/\{(\d)-(\d)\}/.exec(template) || /\{([a-z])-([a-z])\}/.exec(template);
if (match) {
var templates = [];
var startCharCode = match[1].charCodeAt(0);
var stopCharCode = match[2].charCodeAt(0);
var charCode;
for (charCode = startCharCode; charCode <= stopCharCode; ++charCode) {
templates.push(template.replace(match[0], String.fromCharCode(charCode)));
}
};
return ol.TileUrlFunction.createFromTemplates(templates);
} else {
return function(tileCoord) {
if (goog.isNull(tileCoord)) {
return undefined;
} else {
return template.replace('{z}', tileCoord.z)
.replace('{x}', tileCoord.x)
.replace('{y}', tileCoord.y);
}
};
}
};