Skip to content

Commit

Permalink
Merge pull request #7431 from tschaub/defined-zoom
Browse files Browse the repository at this point in the history
Avoid returning undefined zoom
  • Loading branch information
tschaub committed Nov 8, 2017
2 parents be06224 + 967118e commit 3a6fc7a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 23 deletions.
34 changes: 16 additions & 18 deletions src/ol/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -788,8 +788,9 @@ ol.View.prototype.getState = function() {


/**
* Get the current zoom level. Return undefined if the current
* resolution is undefined or not within the "resolution constraints".
* Get the current zoom level. If you configured your view with a resolutions
* array (this is rare), this method may return non-integer zoom levels (so
* the zoom level is not safe to use as an index into a resolutions array).
* @return {number|undefined} Zoom.
* @api
*/
Expand All @@ -810,25 +811,22 @@ ol.View.prototype.getZoom = function() {
* @api
*/
ol.View.prototype.getZoomForResolution = function(resolution) {
var zoom;
if (resolution >= this.minResolution_ && resolution <= this.maxResolution_) {
var offset = this.minZoom_ || 0;
var max, zoomFactor;
if (this.resolutions_) {
var nearest = ol.array.linearFindNearest(this.resolutions_, resolution, 1);
offset += nearest;
if (nearest == this.resolutions_.length - 1) {
return offset;
}
max = this.resolutions_[nearest];
zoomFactor = max / this.resolutions_[nearest + 1];
var offset = this.minZoom_ || 0;
var max, zoomFactor;
if (this.resolutions_) {
var nearest = ol.array.linearFindNearest(this.resolutions_, resolution, 1);
offset += nearest;
max = this.resolutions_[nearest];
if (nearest == this.resolutions_.length - 1) {
zoomFactor = 2;
} else {
max = this.maxResolution_;
zoomFactor = this.zoomFactor_;
zoomFactor = max / this.resolutions_[nearest + 1];
}
zoom = offset + Math.log(max / resolution) / Math.log(zoomFactor);
} else {
max = this.maxResolution_;
zoomFactor = this.zoomFactor_;
}
return zoom;
return offset + Math.log(max / resolution) / Math.log(zoomFactor);
};


Expand Down
10 changes: 5 additions & 5 deletions test/spec/ol/view.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


goog.require('ol');
goog.require('ol.Map');
goog.require('ol.View');
Expand Down Expand Up @@ -943,12 +941,12 @@ describe('ol.View', function() {
});
});

it('returns correct zoom levels', function() {
it('returns correct zoom levels (with resolutions array)', function() {
view.setResolution(undefined);
expect(view.getZoom()).to.be(undefined);

view.setResolution(513);
expect(view.getZoom()).to.be(undefined);
expect(view.getZoom()).to.roughlyEqual(Math.log(512 / 513) / Math.LN2, 1e-9);

view.setResolution(512);
expect(view.getZoom()).to.be(0);
Expand All @@ -966,7 +964,7 @@ describe('ol.View', function() {
expect(view.getZoom()).to.be(5);

view.setResolution(15);
expect(view.getZoom()).to.be(undefined);
expect(view.getZoom()).to.roughlyEqual(Math.log(512 / 15) / Math.LN2, 1e-9);
});

it('works for resolution arrays with variable zoom factors', function() {
Expand Down Expand Up @@ -1070,6 +1068,8 @@ describe('ol.View', function() {
expect(view.getZoomForResolution(max / 2)).to.be(1);

expect(view.getZoomForResolution(max / 4)).to.be(2);

expect(view.getZoomForResolution(2 * max)).to.be(-1);
});

it('returns correct zoom levels for specifically configured resolutions', function() {
Expand Down

0 comments on commit 3a6fc7a

Please sign in to comment.