diff --git a/src/ol/interaction/interaction.js b/src/ol/interaction/interaction.js index 3fdb640b2f..51625a9eb3 100644 --- a/src/ol/interaction/interaction.js +++ b/src/ol/interaction/interaction.js @@ -6,6 +6,7 @@ goog.require('ol'); goog.require('ol.Object'); goog.require('ol.easing'); goog.require('ol.interaction.Property'); +goog.require('ol.math'); /** @@ -180,6 +181,12 @@ ol.interaction.Interaction.zoom = function(view, resolution, opt_anchor, opt_dur ol.interaction.Interaction.zoomByDelta = function(view, delta, opt_anchor, opt_duration) { var currentResolution = view.getResolution(); var resolution = view.constrainResolution(currentResolution, delta, 0); + var resolutions = view.getResolutions(); + + resolution = ol.math.clamp( + resolution, + view.getMinResolution() || resolutions[resolutions.length - 1], + view.getMaxResolution() || resolutions[0]); // If we have a constraint on center, we need to change the anchor so that the // new center is within the extent. We first calculate the new center, apply diff --git a/src/ol/view.js b/src/ol/view.js index 7b5ce16067..0df83f32d5 100644 --- a/src/ol/view.js +++ b/src/ol/view.js @@ -180,6 +180,12 @@ ol.View.prototype.applyOptions_ = function(options) { } else if (options.zoom !== undefined) { properties[ol.ViewProperty.RESOLUTION] = this.constrainResolution( this.maxResolution_, options.zoom - this.minZoom_); + + if (this.resolutions_) { // in case map zoom is out of min/max zoom range + properties[ol.ViewProperty.RESOLUTION] = ol.math.clamp( + Number(this.getResolution() || properties[ol.ViewProperty.RESOLUTION]), + this.minResolution_, this.maxResolution_); + } } properties[ol.ViewProperty.ROTATION] = options.rotation !== undefined ? options.rotation : 0; @@ -815,7 +821,7 @@ ol.View.prototype.getZoomForResolution = function(resolution) { var max, zoomFactor; if (this.resolutions_) { var nearest = ol.array.linearFindNearest(this.resolutions_, resolution, 1); - offset += nearest; + offset = nearest; max = this.resolutions_[nearest]; if (nearest == this.resolutions_.length - 1) { zoomFactor = 2; @@ -1103,8 +1109,9 @@ ol.View.createResolutionConstraint_ = function(options) { if (options.resolutions !== undefined) { var resolutions = options.resolutions; - maxResolution = resolutions[0]; - minResolution = resolutions[resolutions.length - 1]; + maxResolution = resolutions[minZoom]; + minResolution = resolutions[maxZoom] !== undefined ? + resolutions[maxZoom] : resolutions[resolutions.length - 1]; resolutionConstraint = ol.ResolutionConstraint.createSnapToResolutions( resolutions); } else { diff --git a/test/spec/ol/view.test.js b/test/spec/ol/view.test.js index de81d56133..2ee3aeeeba 100644 --- a/test/spec/ol/view.test.js +++ b/test/spec/ol/view.test.js @@ -1147,6 +1147,56 @@ describe('ol.View', function() { }); + describe('#setMaxZoom', function() { + describe('with resolutions property in view', function() { + it('changes the zoom level when the level is over max zoom', function() { + var view = new ol.View({ + resolutions: [100000, 50000, 25000, 12500, 6250, 3125], + zoom: 4 + }); + + view.setMaxZoom(2); + expect(view.getZoom()).to.be(2); + }); + }); + + describe('with no resolutions property in view', function() { + it('changes the zoom level when the level is over max zoom', function() { + var view = new ol.View({ + zoom: 4 + }); + + view.setMaxZoom(2); + expect(view.getZoom()).to.be(2); + }); + }); + }); + + describe('#setMinZoom', function() { + describe('with resolutions property in view', function() { + it('changes the zoom level when the level is under min zoom', function() { + var view = new ol.View({ + resolutions: [100000, 50000, 25000, 12500, 6250, 3125], + zoom: 4 + }); + + view.setMinZoom(5); + expect(view.getZoom()).to.be(5); + }); + }); + + describe('with no resolutions property in view', function() { + it('changes the zoom level when the level is under min zoom', function() { + var view = new ol.View({ + zoom: 4 + }); + + view.setMinZoom(5); + expect(view.getZoom()).to.be(5); + }); + }); + }); + describe('#calculateExtent', function() { it('returns the expected extent', function() { var view = new ol.View({ @@ -1438,5 +1488,4 @@ describe('ol.View.isNoopAnimation()', function() { expect(noop).to.equal(c.noop); }); }); - });