From e9c340b9574d053f4b427d94fc6274fec2463946 Mon Sep 17 00:00:00 2001 From: Tim Schaub Date: Mon, 27 Mar 2017 14:18:16 -0600 Subject: [PATCH] Allow min/max zoom to be set on a view --- src/ol/view.js | 47 ++++++++++++++++++++++++++++ test/spec/ol/view.test.js | 66 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 113 insertions(+) diff --git a/src/ol/view.js b/src/ol/view.js index 89e0bf4270..333efca9f4 100644 --- a/src/ol/view.js +++ b/src/ol/view.js @@ -190,6 +190,33 @@ ol.View.prototype.applyOptions_ = function(options) { }; +/** + * Get an updated version of the view options used to construct the view. The + * current resolution (or zoom), center, and rotation are applied to any stored + * options. The provided options can be uesd to apply new min/max zoom or + * resolution limits. + * @param {olx.ViewOptions} newOptions New options to be applied. + * @return {olx.ViewOptions} New options updated with the current view state. + */ +ol.View.prototype.getUpdatedOptions_ = function(newOptions) { + var options = ol.obj.assign({}, this.options_); + + // preserve resolution (or zoom) + if (options.resolution !== undefined) { + options.resolution = this.getResolution(); + } else { + options.zoom = this.getZoom(); + } + + // preserve center + options.center = this.getCenter(); + + // preserve rotation + options.rotation = this.getRotation(); + + return ol.obj.assign({}, options, newOptions); +}; + /** * Animate the view. The view's center, zoom (or resolution), and rotation @@ -559,6 +586,16 @@ ol.View.prototype.getMaxZoom = function() { }; +/** + * Set a new maximum zoom level for the view. + * @param {number} zoom The maximum zoom level. + * @api + */ +ol.View.prototype.setMaxZoom = function(zoom) { + this.applyOptions_(this.getUpdatedOptions_({maxZoom: zoom})); +}; + + /** * Get the minimum zoom level for the view. * @return {number} The minimum zoom level. @@ -569,6 +606,16 @@ ol.View.prototype.getMinZoom = function() { }; +/** + * Set a new minimum zoom level for the view. + * @param {number} zoom The minimum zoom level. + * @api + */ +ol.View.prototype.setMinZoom = function(zoom) { + this.applyOptions_(this.getUpdatedOptions_({minZoom: zoom})); +}; + + /** * Get the view projection. * @return {ol.proj.Projection} The projection of the view. diff --git a/test/spec/ol/view.test.js b/test/spec/ol/view.test.js index d12505d538..cf06ec2fea 100644 --- a/test/spec/ol/view.test.js +++ b/test/spec/ol/view.test.js @@ -330,6 +330,72 @@ describe('ol.View', function() { }); + describe('#getUpdatedOptions_()', function() { + + it('applies minZoom to constructor options', function() { + var view = new ol.View({ + center: [0, 0], + minZoom: 2, + zoom: 10 + }); + var options = view.getUpdatedOptions_({minZoom: 3}); + + expect(options.center).to.eql([0, 0]); + expect(options.minZoom).to.eql(3); + expect(options.zoom).to.eql(10); + }); + + it('applies the current zoom', function() { + var view = new ol.View({ + center: [0, 0], + zoom: 10 + }); + view.setZoom(8); + var options = view.getUpdatedOptions_(); + + expect(options.center).to.eql([0, 0]); + expect(options.zoom).to.eql(8); + }); + + it('applies the current resolution if resolution was originally supplied', function() { + var view = new ol.View({ + center: [0, 0], + resolution: 1000 + }); + view.setResolution(500); + var options = view.getUpdatedOptions_(); + + expect(options.center).to.eql([0, 0]); + expect(options.resolution).to.eql(500); + }); + + it('applies the current center', function() { + var view = new ol.View({ + center: [0, 0], + zoom: 10 + }); + view.setCenter([1, 2]); + var options = view.getUpdatedOptions_(); + + expect(options.center).to.eql([1, 2]); + expect(options.zoom).to.eql(10); + }); + + it('applies the current rotation', function() { + var view = new ol.View({ + center: [0, 0], + zoom: 10 + }); + view.setRotation(Math.PI / 6); + var options = view.getUpdatedOptions_(); + + expect(options.center).to.eql([0, 0]); + expect(options.zoom).to.eql(10); + expect(options.rotation).to.eql(Math.PI / 6); + }); + + }); + describe('#animate()', function() { var originalRequestAnimationFrame = window.requestAnimationFrame;