Make the resolution constraint configurable through map options
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
// FIXME rotation constraint is not configurable at the moment
|
||||
|
||||
goog.provide('ol.MapOptions');
|
||||
goog.provide('ol.MapOptionsLiteral');
|
||||
goog.provide('ol.MapOptionsType');
|
||||
@@ -171,9 +173,27 @@ ol.MapOptions.create = function(mapOptionsLiteral) {
|
||||
* @return {ol.Constraints} Map constraints.
|
||||
*/
|
||||
ol.MapOptions.createConstraints_ = function(mapOptionsLiteral) {
|
||||
// FIXME this should be configurable
|
||||
var resolutionConstraint = ol.ResolutionConstraint.createSnapToPower(
|
||||
Math.exp(Math.log(2) / 4), ol.Projection.EPSG_3857_HALF_SIZE / 128);
|
||||
var resolutionConstraint;
|
||||
if (goog.isDef(mapOptionsLiteral.resolutions)) {
|
||||
resolutionConstraint = ol.ResolutionConstraint.createSnapToResolutions(
|
||||
mapOptionsLiteral.resolutions);
|
||||
} else {
|
||||
var maxResolution, numZoomLevels, zoomFactor;
|
||||
if (goog.isDef(mapOptionsLiteral.maxResolution) &&
|
||||
goog.isDef(mapOptionsLiteral.numZoomLevels) &&
|
||||
goog.isDef(mapOptionsLiteral.zoomFactor)) {
|
||||
maxResolution = mapOptionsLiteral.maxResolution;
|
||||
numZoomLevels = mapOptionsLiteral.numZoomLevels;
|
||||
zoomFactor = mapOptionsLiteral.zoomFactor;
|
||||
} else {
|
||||
maxResolution = ol.Projection.EPSG_3857_HALF_SIZE / 128;
|
||||
numZoomLevels = 29;
|
||||
zoomFactor = Math.exp(Math.log(2) / 4);
|
||||
}
|
||||
resolutionConstraint = ol.ResolutionConstraint.createSnapToPower(
|
||||
zoomFactor, maxResolution, numZoomLevels - 1);
|
||||
}
|
||||
// FIXME rotation constraint is not configurable at the moment
|
||||
var rotationConstraint = ol.RotationConstraint.none;
|
||||
return new ol.Constraints(resolutionConstraint, rotationConstraint);
|
||||
};
|
||||
|
||||
@@ -76,6 +76,7 @@
|
||||
<script type="text/javascript" src="spec/ol/object.test.js"></script>
|
||||
<script type="text/javascript" src="spec/ol/tilecoord.test.js"></script>
|
||||
<script type="text/javascript" src="spec/ol/tilegrid.test.js"></script>
|
||||
<script type="text/javascript" src="spec/ol/mapoptions.test.js"></script>
|
||||
<script type="text/javascript" src="spec/ol/projection.test.js"></script>
|
||||
<script type="text/javascript" src="spec/ol/rectangle.test.js"></script>
|
||||
<script type="text/javascript" src="spec/ol/tilerange.test.js"></script>
|
||||
|
||||
54
test/spec/ol/mapoptions.test.js
Normal file
54
test/spec/ol/mapoptions.test.js
Normal file
@@ -0,0 +1,54 @@
|
||||
goog.require('ol.MapOptions');
|
||||
|
||||
describe('ol.MapOptions', function() {
|
||||
|
||||
describe('create constraints', function() {
|
||||
|
||||
describe('create resolution constraint', function() {
|
||||
|
||||
describe('with no options', function() {
|
||||
it('gives a correct resolution constraint function', function() {
|
||||
var options = {};
|
||||
var fn = ol.MapOptions.createConstraints_(options).resolution;
|
||||
expect(fn(156543.03392804097, 0))
|
||||
.toRoughlyEqual(156543.03392804097, 1e-9);
|
||||
expect(fn(78271.51696402048, 0))
|
||||
.toRoughlyEqual(78271.51696402048, 1e-10);
|
||||
});
|
||||
});
|
||||
|
||||
describe('with maxResolution, numZoomLevels, and zoomFactor options',
|
||||
function() {
|
||||
it('gives a correct resolution constraint function', function() {
|
||||
var options = {
|
||||
maxResolution: 81,
|
||||
numZoomLevels: 4,
|
||||
zoomFactor: 3
|
||||
};
|
||||
var fn = ol.MapOptions.createConstraints_(options).resolution;
|
||||
expect(fn(82, 0)).toEqual(81);
|
||||
expect(fn(81, 0)).toEqual(81);
|
||||
expect(fn(27, 0)).toEqual(27);
|
||||
expect(fn(9, 0)).toEqual(9);
|
||||
expect(fn(3, 0)).toEqual(3);
|
||||
expect(fn(2, 0)).toEqual(3);
|
||||
});
|
||||
});
|
||||
|
||||
describe('with resolutions', function() {
|
||||
it('gives a correct resolution constraint function', function() {
|
||||
var options = {
|
||||
resolutions: [97, 76, 65, 54, 0.45]
|
||||
};
|
||||
var fn = ol.MapOptions.createConstraints_(options).resolution;
|
||||
expect(fn(97, 0), 97);
|
||||
expect(fn(76, 0), 76);
|
||||
expect(fn(65, 0), 65);
|
||||
expect(fn(54, 0), 54);
|
||||
expect(fn(0.45, 0), 0.45);
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user