Update calculateInRange: Only turn layers off if they have some scale-related
properties set directly in their creation options. By default, layers will be "always on", with an overridable "alwaysInRange" parameter. To maintain the old behavior, set: OpenLayers.Layer.prototype.alwaysInRange = false; r=euzuro (Closes #987) git-svn-id: http://svn.openlayers.org/trunk/openlayers@7863 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
+56
-5
@@ -37,6 +37,23 @@ OpenLayers.Layer = OpenLayers.Class({
|
|||||||
*/
|
*/
|
||||||
opacity: null,
|
opacity: null,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* APIProperty: alwaysInRange
|
||||||
|
* {Boolean} If a layer's display should not be scale-based, this should
|
||||||
|
* be set to true. This will cause the layer, as an overlay, to always
|
||||||
|
* be 'active', by always returning true from the calculateInRange()
|
||||||
|
* function.
|
||||||
|
*
|
||||||
|
* If not explicitly specified for a layer, its value will be
|
||||||
|
* determined on startup in initResolutions() based on whether or not
|
||||||
|
* any scale-specific properties have been set as options on the
|
||||||
|
* layer. If no scale-specific options have been set on the layer, we
|
||||||
|
* assume that it should always be in range.
|
||||||
|
*
|
||||||
|
* See #987 for more info.
|
||||||
|
*/
|
||||||
|
alwaysInRange: null,
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constant: EVENT_TYPES
|
* Constant: EVENT_TYPES
|
||||||
* {Array(String)} Supported application event types. Register a listener
|
* {Array(String)} Supported application event types. Register a listener
|
||||||
@@ -615,14 +632,20 @@ OpenLayers.Layer = OpenLayers.Class({
|
|||||||
*
|
*
|
||||||
* Returns:
|
* Returns:
|
||||||
* {Boolean} The layer is displayable at the current map's current
|
* {Boolean} The layer is displayable at the current map's current
|
||||||
* resolution.
|
* resolution. Note that if 'alwaysInRange' is true for the layer,
|
||||||
|
* this function will always return true.
|
||||||
*/
|
*/
|
||||||
calculateInRange: function() {
|
calculateInRange: function() {
|
||||||
var inRange = false;
|
var inRange = false;
|
||||||
if (this.map) {
|
|
||||||
var resolution = this.map.getResolution();
|
if (this.alwaysInRange) {
|
||||||
inRange = ( (resolution >= this.minResolution) &&
|
inRange = true;
|
||||||
(resolution <= this.maxResolution) );
|
} else {
|
||||||
|
if (this.map) {
|
||||||
|
var resolution = this.map.getResolution();
|
||||||
|
inRange = ( (resolution >= this.minResolution) &&
|
||||||
|
(resolution <= this.maxResolution) );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return inRange;
|
return inRange;
|
||||||
},
|
},
|
||||||
@@ -677,6 +700,15 @@ OpenLayers.Layer = OpenLayers.Class({
|
|||||||
'numZoomLevels', 'maxZoomLevel'
|
'numZoomLevels', 'maxZoomLevel'
|
||||||
);
|
);
|
||||||
|
|
||||||
|
//these are the properties which do *not* imply that user wishes
|
||||||
|
// this layer to be scale-dependant
|
||||||
|
var notScaleProps = ['projection', 'units'];
|
||||||
|
|
||||||
|
//should the layer be scale-dependant? default is false -- this will
|
||||||
|
// only be set true if we find that the user has specified a property
|
||||||
|
// from the 'props' array that is not in 'notScaleProps'
|
||||||
|
var useInRange = false;
|
||||||
|
|
||||||
// First we create a new object where we will store all of the
|
// First we create a new object where we will store all of the
|
||||||
// resolution-related properties that we find in either the layer's
|
// resolution-related properties that we find in either the layer's
|
||||||
// 'options' array or from the map.
|
// 'options' array or from the map.
|
||||||
@@ -684,9 +716,28 @@ OpenLayers.Layer = OpenLayers.Class({
|
|||||||
var confProps = {};
|
var confProps = {};
|
||||||
for(var i=0, len=props.length; i<len; i++) {
|
for(var i=0, len=props.length; i<len; i++) {
|
||||||
var property = props[i];
|
var property = props[i];
|
||||||
|
|
||||||
|
// If the layer had one of these properties set *and* it is
|
||||||
|
// a scale property (is not a non-scale property), then we assume
|
||||||
|
// the user did intend to use scale-dependant display (useInRange).
|
||||||
|
if (this.options[property] &&
|
||||||
|
OpenLayers.Util.indexOf(notScaleProps, property) == -1) {
|
||||||
|
useInRange = true;
|
||||||
|
}
|
||||||
|
|
||||||
confProps[property] = this.options[property] || this.map[property];
|
confProps[property] = this.options[property] || this.map[property];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//only automatically set 'alwaysInRange' if the user hasn't already
|
||||||
|
// set it (to true or false, since the default is null). If user did
|
||||||
|
// not intend to use scale-dependant display then we set they layer
|
||||||
|
// as alwaysInRange. This means calculateInRange() will always return
|
||||||
|
// true and the layer will never be turned off due to scale changes.
|
||||||
|
//
|
||||||
|
if (this.alwaysInRange == null) {
|
||||||
|
this.alwaysInRange = !useInRange;
|
||||||
|
}
|
||||||
|
|
||||||
// Do not use the scales array set at the map level if
|
// Do not use the scales array set at the map level if
|
||||||
// either minScale or maxScale or both are set at the
|
// either minScale or maxScale or both are set at the
|
||||||
// layer level
|
// layer level
|
||||||
|
|||||||
+14
-3
@@ -145,11 +145,11 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
function test_Layer_initResolutions(t) {
|
function test_Layer_initResolutions(t) {
|
||||||
t.plan(12);
|
t.plan(15);
|
||||||
var map = new OpenLayers.Map("map");
|
var map = new OpenLayers.Map("map");
|
||||||
var options, layer;
|
var options, layer;
|
||||||
|
|
||||||
// three tests for minResolution, maxResolution, and numZoomLevels
|
// tests for minResolution, maxResolution, and numZoomLevels
|
||||||
options = {
|
options = {
|
||||||
minResolution: 1.5,
|
minResolution: 1.5,
|
||||||
maxResolution: 10.5,
|
maxResolution: 10.5,
|
||||||
@@ -163,7 +163,8 @@
|
|||||||
t.eq(layer.maxResolution.toPrecision(6), (10.5).toPrecision(6),
|
t.eq(layer.maxResolution.toPrecision(6), (10.5).toPrecision(6),
|
||||||
"(with numZoomLevels) layer maxResolution preserved");
|
"(with numZoomLevels) layer maxResolution preserved");
|
||||||
t.eq(layer.numZoomLevels, 5, "(with numZoomLevels) layer numZoomLevels preserved");
|
t.eq(layer.numZoomLevels, 5, "(with numZoomLevels) layer numZoomLevels preserved");
|
||||||
|
t.eq(layer.alwaysInRange, false, "Always in range is set to false due to passed options.")
|
||||||
|
|
||||||
// three tests for minResolution, and maxResolution
|
// three tests for minResolution, and maxResolution
|
||||||
options = {
|
options = {
|
||||||
minResolution: 1.5,
|
minResolution: 1.5,
|
||||||
@@ -206,6 +207,16 @@
|
|||||||
t.eq(layer.maxScale.toPrecision(6), (155).toPrecision(6),
|
t.eq(layer.maxScale.toPrecision(6), (155).toPrecision(6),
|
||||||
"(without numZoomLevels) layer maxScale preserved");
|
"(without numZoomLevels) layer maxScale preserved");
|
||||||
t.eq(layer.numZoomLevels, 4, "(without numZoomLevels) layer numZoomLevels calculated");
|
t.eq(layer.numZoomLevels, 4, "(without numZoomLevels) layer numZoomLevels calculated");
|
||||||
|
|
||||||
|
layer = new OpenLayers.Layer("test", {'projection': 'EPSG:4326', 'map': map});
|
||||||
|
layer.initResolutions();
|
||||||
|
t.eq(layer.alwaysInRange, true, "always in range true if only get projection.");
|
||||||
|
|
||||||
|
OpenLayers.Layer.prototype.alwaysInRange = false;
|
||||||
|
layer = new OpenLayers.Layer("test", {'projection': 'EPSG:4326', 'map': map});
|
||||||
|
layer.initResolutions();
|
||||||
|
t.eq(layer.alwaysInRange, false, "always in range true if overridden on prototype.");
|
||||||
|
OpenLayers.Layer.prototype.alwaysInRange = null;
|
||||||
|
|
||||||
map.destroy();
|
map.destroy();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user