Merge pull request #1011 from twpayne/set-url

Add ol.source.XYZ#setUrl
This commit is contained in:
Tom Payne
2013-09-11 09:17:07 -07:00
5 changed files with 160 additions and 24 deletions

62
examples/xyz-seturl.html Normal file
View File

@@ -0,0 +1,62 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="chrome=1">
<meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
<link rel="stylesheet" href="../css/ol.css" type="text/css">
<link rel="stylesheet" href="../resources/bootstrap/css/bootstrap.min.css" type="text/css">
<link rel="stylesheet" href="../resources/layout.css" type="text/css">
<link rel="stylesheet" href="../resources/bootstrap/css/bootstrap-responsive.min.css" type="text/css">
<title>SetURL example</title>
</head>
<body>
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<a class="brand" href="./">OpenLayers 3 Examples</a>
<ul class="nav pull-right">
<li><iframe class="github-watch-button" src="http://ghbtns.com/github-btn.html?user=openlayers&repo=ol3&type=watch&count=true"
allowtransparency="true" frameborder="0" scrolling="0" height="20" width="90"></iframe></li>
<li><a href="https://twitter.com/share" class="twitter-share-button" data-count="none" data-hashtags="openlayers">&nbsp;</a></li>
<li><div class="g-plusone-wrapper"><div class="g-plusone" data-size="medium" data-annotation="none"></div></div></li>
</ul>
</div>
</div>
</div>
<div class="container-fluid">
<div class="row-fluid">
<div class="span12">
<div id="map" class="map"></div>
<button id="set-stamen">Stamen Watercolor</button>
<button id="set-opencyclemap">OpenCycleMap</button>
<button id="set-esri">ESRI World Topo Map</button>
</div>
</div>
<div class="row-fluid">
<div class="span12">
<h4 id="title">XYZ set URL example</h4>
<p id="shortdesc">Example of a setting the URL on an XYZ source</p>
<div id="docs">
<p>See the <a href="xyz-seturl.js" target="_blank">xyz-seturl.js source</a> to see how this is done.</p>
</div>
<div id="tags">xyz, seturl</div>
</div>
</div>
</div>
<script src="jquery.min.js" type="text/javascript"></script>
<script src="loader.js?id=xyz-seturl" type="text/javascript"></script>
<script src="../resources/social-links.js" type="text/javascript"></script>
</body>
</html>

34
examples/xyz-seturl.js Normal file
View File

@@ -0,0 +1,34 @@
goog.require('ol.Map');
goog.require('ol.RendererHints');
goog.require('ol.View2D');
goog.require('ol.layer.Tile');
goog.require('ol.source.XYZ');
var source = new ol.source.XYZ({
url: 'http://{a-c}.tile.openstreetmap.org/{z}/{x}/{y}.png'
});
var map = new ol.Map({
layers: [
new ol.layer.Tile({
source: source
})
],
renderers: ol.RendererHints.createFromQueryData(),
target: 'map',
view: new ol.View2D({
center: [0, 0],
zoom: 4
})
});
$('#set-stamen').click(function() {
source.setUrl('http://{a-d}.tile.stamen.com/watercolor/{z}/{x}/{y}.jpg');
});
$('#set-opencyclemap').click(function() {
source.setUrl('http://{a-c}.tile.opencyclemap.org/cycle/{z}/{x}/{y}.png');
});
$('#set-esri').click(function() {
source.setUrl('http://server.arcgisonline.com/ArcGIS/rest/services/' +
'World_Topo_Map/MapServer/tile/{z}/{y}/{x}');
});

View File

@@ -108,6 +108,19 @@ ol.source.TileImage.prototype.getTile = function(z, x, y, projection) {
};
/**
* @param {ol.TileUrlFunctionType} tileUrlFunction Tile URL function.
*/
ol.source.TileImage.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();
this.tileUrlFunction = tileUrlFunction;
this.dispatchChangeEvent();
};
/**
* @inheritDoc
*/

View File

@@ -18,33 +18,12 @@ ol.source.XYZ = function(options) {
var projection = options.projection || ol.proj.get('EPSG:3857');
/**
* @type {ol.TileUrlFunctionType}
*/
var tileUrlFunction = ol.TileUrlFunction.nullTileUrlFunction;
// FIXME use goog.nullFunction ?
if (goog.isDef(options.tileUrlFunction)) {
tileUrlFunction = options.tileUrlFunction;
} else if (goog.isDef(options.urls)) {
tileUrlFunction = ol.TileUrlFunction.createFromTemplates(options.urls);
} else if (goog.isDef(options.url)) {
tileUrlFunction = ol.TileUrlFunction.createFromTemplates(
ol.TileUrlFunction.expandUrl(options.url));
}
var maxZoom = goog.isDef(options.maxZoom) ? options.maxZoom : 18;
var tileGrid = new ol.tilegrid.XYZ({
maxZoom: maxZoom
});
var tileCoordTransform = tileGrid.createTileCoordTransform({
extent: options.extent
});
tileUrlFunction = ol.TileUrlFunction.withTileCoordTransform(
tileCoordTransform, tileUrlFunction);
goog.base(this, {
attributions: options.attributions,
crossOrigin: options.crossOrigin,
@@ -52,8 +31,51 @@ ol.source.XYZ = function(options) {
logo: options.logo,
projection: projection,
tileGrid: tileGrid,
tileUrlFunction: tileUrlFunction
tileUrlFunction: ol.TileUrlFunction.nullTileUrlFunction
});
/**
* @private
* @type {ol.TileCoordTransformType}
*/
this.tileCoordTransform_ = tileGrid.createTileCoordTransform({
extent: options.extent
});
if (goog.isDef(options.tileUrlFunction)) {
this.setTileUrlFunction(options.tileUrlFunction);
} else if (goog.isDef(options.urls)) {
this.setUrls(options.urls);
} else if (goog.isDef(options.url)) {
this.setUrl(options.url);
}
};
goog.inherits(ol.source.XYZ, ol.source.TileImage);
/**
* @param {ol.TileUrlFunctionType} tileUrlFunction Tile URL function.
*/
ol.source.XYZ.prototype.setTileUrlFunction = function(tileUrlFunction) {
goog.base(this, 'setTileUrlFunction',
ol.TileUrlFunction.withTileCoordTransform(
this.tileCoordTransform_, tileUrlFunction));
};
/**
* @param {string} url URL.
*/
ol.source.XYZ.prototype.setUrl = function(url) {
this.setTileUrlFunction(ol.TileUrlFunction.createFromTemplates(
ol.TileUrlFunction.expandUrl(url)));
};
/**
* @param {Array.<string>} urls URLs.
*/
ol.source.XYZ.prototype.setUrls = function(urls) {
this.setTileUrlFunction(ol.TileUrlFunction.createFromTemplates(urls));
};

View File

@@ -14,6 +14,12 @@ goog.require('ol.extent');
ol.TileUrlFunctionType;
/**
* @typedef {function(ol.TileCoord, ol.Projection, ol.TileCoord=): ol.TileCoord}
*/
ol.TileCoordTransformType;
/**
* @param {string} template Template.
* @return {ol.TileUrlFunctionType} Tile URL function.
@@ -121,8 +127,7 @@ ol.TileUrlFunction.nullTileUrlFunction = function(tileCoord, projection) {
/**
* @param {function(ol.TileCoord, ol.Projection, ol.TileCoord=): ol.TileCoord}
* transformFn Transform function.
* @param {ol.TileCoordTransformType} transformFn Transform function.
* @param {ol.TileUrlFunctionType} tileUrlFunction Tile URL function.
* @return {ol.TileUrlFunctionType} Tile URL function.
*/