diff --git a/src/ol/layer/TileLayer.js b/src/ol/layer/TileLayer.js index 320c586d1a..12cd2242ff 100644 --- a/src/ol/layer/TileLayer.js +++ b/src/ol/layer/TileLayer.js @@ -153,15 +153,44 @@ ol.layer.TileLayer.prototype.getTileOrigin = function() { return null; }; +/** + * Get max resolution. Return undefined if the layer has no maxResolution, + * and no extent from which maxResolution could be derived. + * @return {number|undefined} + */ +ol.layer.TileLayer.prototype.getMaxResolution = function() { + if (!goog.isDef(this.maxResolution_)) { + var extent = this.getExtent(); + if (!goog.isNull(extent)) { + this.maxResolution_ = Math.max( + (extent.getMaxX() - extent.getMinX()) / this.tileWidth_, + (extent.getMaxY() - extent.getMaxX()) / this.tileHeight_); + } + } + return this.maxResolution_; +}; + +/** + * Get the number of the zoom levels. + * @return {number|undefined} + */ +ol.layer.TileLayer.prototype.getNumZoomLevels = function() { + return this.numZoomLevels_; +}; + /** * Get layer resolutions. Return null if the layer has no resolutions. * @return {Array.} */ ol.layer.TileLayer.prototype.getResolutions = function() { - if (goog.isNull(this.resolutions_) && goog.isDef(this.maxResolution_)) { - this.resolutions_ = []; - for (var i = 0; i < this.numZoomLevels_; i++) { - this.resolutions_[i] = this.maxResolution_ / Math.pow(2, i); + if (goog.isNull(this.resolutions_)) { + var maxResolution = this.getMaxResolution(), + numZoomLevels = this.getNumZoomLevels(); + if (goog.isDef(maxResolution) && goog.isDef(numZoomLevels)) { + this.resolutions_ = []; + for (var i = 0; i < numZoomLevels; i++) { + this.resolutions_[i] = maxResolution / Math.pow(2, i); + } } } return this.resolutions_; diff --git a/src/ol/layer/XYZ.js b/src/ol/layer/XYZ.js index c32ea74a50..6d6ea12485 100644 --- a/src/ol/layer/XYZ.js +++ b/src/ol/layer/XYZ.js @@ -18,7 +18,8 @@ ol.layer.XYZ = function(url) { goog.base(this); this.setUrl(url); - this.setMaxResolution(156543.03390625); + this.setProjection(new ol.Projection("EPSG:3857")); + this.setNumZoomLevels(22); }; goog.inherits(ol.layer.XYZ, ol.layer.TileLayer); diff --git a/test/spec/ol/layer/TileLayer.test.js b/test/spec/ol/layer/TileLayer.test.js index cd7706e06e..8cce9a34e9 100644 --- a/test/spec/ol/layer/TileLayer.test.js +++ b/test/spec/ol/layer/TileLayer.test.js @@ -134,6 +134,38 @@ describe('ol.layer.TileLayer', function() { }); }); + describe('get max resolution', function() { + var layer; + + beforeEach(function() { + layer = new ol.layer.TileLayer(); + }); + + describe('with max resolution', function() { + + beforeEach(function() { + layer.setMaxResolution(156543.03390625); + }); + + it('returns the expected maxResolution', function() { + var maxResolution = layer.getMaxResolution(); + expect(maxResolution).toEqual(156543.03390625); + }); + }); + + describe('with projection', function() { + + beforeEach(function() { + layer.setProjection(new ol.Projection("EPSG:3857")); + }); + + it('returns the expected maxResolution', function() { + var maxResolution = layer.getMaxResolution(); + expect(maxResolution).toEqual(156543.03390625); + }); + }); + }); + describe('get data from the layer', function() { var layer;