Merge pull request #7431 from tschaub/defined-zoom
Avoid returning undefined zoom
This commit is contained in:
+16
-18
@@ -788,8 +788,9 @@ ol.View.prototype.getState = function() {
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the current zoom level. Return undefined if the current
|
* Get the current zoom level. If you configured your view with a resolutions
|
||||||
* resolution is undefined or not within the "resolution constraints".
|
* 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.
|
* @return {number|undefined} Zoom.
|
||||||
* @api
|
* @api
|
||||||
*/
|
*/
|
||||||
@@ -810,25 +811,22 @@ ol.View.prototype.getZoom = function() {
|
|||||||
* @api
|
* @api
|
||||||
*/
|
*/
|
||||||
ol.View.prototype.getZoomForResolution = function(resolution) {
|
ol.View.prototype.getZoomForResolution = function(resolution) {
|
||||||
var zoom;
|
var offset = this.minZoom_ || 0;
|
||||||
if (resolution >= this.minResolution_ && resolution <= this.maxResolution_) {
|
var max, zoomFactor;
|
||||||
var offset = this.minZoom_ || 0;
|
if (this.resolutions_) {
|
||||||
var max, zoomFactor;
|
var nearest = ol.array.linearFindNearest(this.resolutions_, resolution, 1);
|
||||||
if (this.resolutions_) {
|
offset += nearest;
|
||||||
var nearest = ol.array.linearFindNearest(this.resolutions_, resolution, 1);
|
max = this.resolutions_[nearest];
|
||||||
offset += nearest;
|
if (nearest == this.resolutions_.length - 1) {
|
||||||
if (nearest == this.resolutions_.length - 1) {
|
zoomFactor = 2;
|
||||||
return offset;
|
|
||||||
}
|
|
||||||
max = this.resolutions_[nearest];
|
|
||||||
zoomFactor = max / this.resolutions_[nearest + 1];
|
|
||||||
} else {
|
} else {
|
||||||
max = this.maxResolution_;
|
zoomFactor = max / this.resolutions_[nearest + 1];
|
||||||
zoomFactor = this.zoomFactor_;
|
|
||||||
}
|
}
|
||||||
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);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,3 @@
|
|||||||
|
|
||||||
|
|
||||||
goog.require('ol');
|
goog.require('ol');
|
||||||
goog.require('ol.Map');
|
goog.require('ol.Map');
|
||||||
goog.require('ol.View');
|
goog.require('ol.View');
|
||||||
@@ -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);
|
view.setResolution(undefined);
|
||||||
expect(view.getZoom()).to.be(undefined);
|
expect(view.getZoom()).to.be(undefined);
|
||||||
|
|
||||||
view.setResolution(513);
|
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);
|
view.setResolution(512);
|
||||||
expect(view.getZoom()).to.be(0);
|
expect(view.getZoom()).to.be(0);
|
||||||
@@ -966,7 +964,7 @@ describe('ol.View', function() {
|
|||||||
expect(view.getZoom()).to.be(5);
|
expect(view.getZoom()).to.be(5);
|
||||||
|
|
||||||
view.setResolution(15);
|
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() {
|
it('works for resolution arrays with variable zoom factors', function() {
|
||||||
@@ -1070,6 +1068,8 @@ describe('ol.View', function() {
|
|||||||
expect(view.getZoomForResolution(max / 2)).to.be(1);
|
expect(view.getZoomForResolution(max / 2)).to.be(1);
|
||||||
|
|
||||||
expect(view.getZoomForResolution(max / 4)).to.be(2);
|
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() {
|
it('returns correct zoom levels for specifically configured resolutions', function() {
|
||||||
|
|||||||
Reference in New Issue
Block a user