UNPKG

openlayers

Version:

Build tools and sources for developing OpenLayers based mapping applications

194 lines (165 loc) 4.71 kB
goog.provide('ol.source.UrlTile'); goog.require('ol'); goog.require('ol.Tile'); goog.require('ol.TileUrlFunction'); goog.require('ol.source.Tile'); /** * @classdesc * Base class for sources providing tiles divided into a tile grid over http. * * @constructor * @fires ol.source.Tile.Event * @extends {ol.source.Tile} * @param {ol.SourceUrlTileOptions} options Image tile options. */ ol.source.UrlTile = function(options) { ol.source.Tile.call(this, { attributions: options.attributions, cacheSize: options.cacheSize, extent: options.extent, logo: options.logo, opaque: options.opaque, projection: options.projection, state: options.state, tileGrid: options.tileGrid, tilePixelRatio: options.tilePixelRatio, wrapX: options.wrapX }); /** * @protected * @type {ol.TileLoadFunctionType} */ this.tileLoadFunction = options.tileLoadFunction; /** * @protected * @type {ol.TileUrlFunctionType} */ this.tileUrlFunction = this.fixedTileUrlFunction ? this.fixedTileUrlFunction.bind(this) : ol.TileUrlFunction.nullTileUrlFunction; /** * @protected * @type {!Array.<string>|null} */ this.urls = null; if (options.urls) { this.setUrls(options.urls); } else if (options.url) { this.setUrl(options.url); } if (options.tileUrlFunction) { this.setTileUrlFunction(options.tileUrlFunction); } }; ol.inherits(ol.source.UrlTile, ol.source.Tile); /** * @type {ol.TileUrlFunctionType|undefined} * @protected */ ol.source.UrlTile.prototype.fixedTileUrlFunction; /** * Return the tile load function of the source. * @return {ol.TileLoadFunctionType} TileLoadFunction * @api */ ol.source.UrlTile.prototype.getTileLoadFunction = function() { return this.tileLoadFunction; }; /** * Return the tile URL function of the source. * @return {ol.TileUrlFunctionType} TileUrlFunction * @api */ ol.source.UrlTile.prototype.getTileUrlFunction = function() { return this.tileUrlFunction; }; /** * Return the URLs used for this source. * When a tileUrlFunction is used instead of url or urls, * null will be returned. * @return {!Array.<string>|null} URLs. * @api */ ol.source.UrlTile.prototype.getUrls = function() { return this.urls; }; /** * Handle tile change events. * @param {ol.events.Event} event Event. * @protected */ ol.source.UrlTile.prototype.handleTileChange = function(event) { var tile = /** @type {ol.Tile} */ (event.target); switch (tile.getState()) { case ol.Tile.State.LOADING: this.dispatchEvent( new ol.source.Tile.Event(ol.source.Tile.EventType.TILELOADSTART, tile)); break; case ol.Tile.State.LOADED: this.dispatchEvent( new ol.source.Tile.Event(ol.source.Tile.EventType.TILELOADEND, tile)); break; case ol.Tile.State.ERROR: this.dispatchEvent( new ol.source.Tile.Event(ol.source.Tile.EventType.TILELOADERROR, tile)); break; default: // pass } }; /** * Set the tile load function of the source. * @param {ol.TileLoadFunctionType} tileLoadFunction Tile load function. * @api */ ol.source.UrlTile.prototype.setTileLoadFunction = function(tileLoadFunction) { this.tileCache.clear(); this.tileLoadFunction = tileLoadFunction; this.changed(); }; /** * 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, opt_key) { this.tileUrlFunction = tileUrlFunction; if (typeof opt_key !== 'undefined') { this.setKey(opt_key); } else { this.changed(); } }; /** * Set the URL to use for requests. * @param {string} url URL. * @api stable */ 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), url); }; /** * Set the URLs to use for requests. * @param {Array.<string>} urls URLs. * @api stable */ 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), key); }; /** * @inheritDoc */ ol.source.UrlTile.prototype.useTile = function(z, x, y) { var tileCoordKey = this.getKeyZXY(z, x, y); if (this.tileCache.containsKey(tileCoordKey)) { this.tileCache.get(tileCoordKey); } };