Don't check based on layer.maxExtent.

layer.maxExtent is always set as soon as the layer is added to a map. Instead, making behavior consistent with tiled layers: don't display outside maxExtent except when displayOutsideMaxExtent is set to true or the layer's extent equals the world bounds for maps with a baseLayer that has wrapDateLine set to true.
This commit is contained in:
ahocevar
2012-03-09 23:50:09 +01:00
parent 0ff2f9a457
commit e52c97f741
2 changed files with 34 additions and 17 deletions

View File

@@ -782,28 +782,32 @@ OpenLayers.Layer.Grid = OpenLayers.Class(OpenLayers.Layer.HTTPRequest, {
var tileWidth = bounds.getWidth() * this.ratio; var tileWidth = bounds.getWidth() * this.ratio;
var tileHeight = bounds.getHeight() * this.ratio; var tileHeight = bounds.getHeight() * this.ratio;
var tileBounds = var tileBounds =
new OpenLayers.Bounds( new OpenLayers.Bounds(center.lon - (tileWidth/2),
center.lon - (tileWidth / 2), center.lat - (tileHeight/2),
center.lat - (tileHeight / 2), center.lon + (tileWidth/2),
center.lon + (tileWidth / 2), center.lat + (tileHeight/2));
center.lat + (tileHeight / 2)
); // adjust tile bounds so they do not exceed maxExtent, except when the
//adjust tile bounds to fit in maxExtent restriction // layer's maxExtent equals the world bounds or displayOutsideMaxExtent
//if there is a maxExtent restriction // is set to true
if(this.maxExtent) { var ignoreMaxExtent =
(this.map.baseLayer.wrapDateLine &&
this.maxExtent.equals(this.map.getMaxExtent())) ||
this.displayOutsideMaxExtent;
if(!ignoreMaxExtent) {
tileBounds.bottom = Math.max(this.maxExtent.bottom, tileBounds.bottom); tileBounds.bottom = Math.max(this.maxExtent.bottom, tileBounds.bottom);
tileBounds.top = Math.min(this.maxExtent.top, tileBounds.top); tileBounds.top = Math.min(this.maxExtent.top, tileBounds.top);
tileBounds.left = Math.max(this.maxExtent.left, tileBounds.left); tileBounds.left = Math.max(this.maxExtent.left, tileBounds.left);
tileBounds.right = Math.min(this.maxExtent.right, tileBounds.right); tileBounds.right = Math.min(this.maxExtent.right, tileBounds.right);
tileWidth = tileBounds.getWidth(); tileWidth = tileBounds.getWidth();
tileHeight = tileBounds.getHeight(); tileHeight = tileBounds.getHeight();
var resolution = this.map.getResolution();
this.tileSize = new OpenLayers.Size(
tileWidth / resolution,
tileHeight / resolution
);
} }
var resolution = this.map.getResolution(),
size = this.tileSize;
size.w = (tileWidth / resolution) | 0;
size.h = (tileHeight / resolution) | 0;
var px = this.map.getLayerPxFromLonLat({ var px = this.map.getLayerPxFromLonLat({
lon: tileBounds.left, lon: tileBounds.left,

View File

@@ -449,7 +449,7 @@
} }
function test_Layer_Grid_initSingleTile(t) { function test_Layer_Grid_initSingleTile(t) {
t.plan( 19 ); t.plan( 24 );
layer = new OpenLayers.Layer.Grid(name, url, params, { layer = new OpenLayers.Layer.Grid(name, url, params, {
singleTile: true, singleTile: true,
@@ -462,13 +462,17 @@
var desiredUL = new OpenLayers.LonLat(-40,145); var desiredUL = new OpenLayers.LonLat(-40,145);
translatedPX = {}; translatedPX = {};
layer.tileSize = new OpenLayers.Size();
layer.map = { layer.map = {
baseLayer: {wrapDateLine: true},
getMaxExtent: function() { return new OpenLayers.Bounds(-180,-90,180,90); },
getLayerPxFromLonLat: function(ul) { getLayerPxFromLonLat: function(ul) {
t.ok(ul.lon === desiredUL.lon && ul.lat === desiredUL.lat, "correct ul passed to translation"); t.ok(ul.lon === desiredUL.lon && ul.lat === desiredUL.lat, "correct ul passed to translation");
return translatedPX; return translatedPX;
}, },
getResolution:function(){return 1;} getResolution:function(){return 1;}
}; };
layer.maxExtent = layer.map.getMaxExtent();
var newTile = { var newTile = {
draw: function() { draw: function() {
@@ -498,7 +502,7 @@
t.ok(tileBounds.equals(desiredTileBounds), "correct tile bounds passed to tile.moveTo()"); t.ok(tileBounds.equals(desiredTileBounds), "correct tile bounds passed to tile.moveTo()");
t.ok(px == translatedPX, "correct tile px passed to tile.moveTo()"); t.ok(px == translatedPX, "correct tile px passed to tile.moveTo()");
} }
}; };
layer.grid = [[ tile ]]; layer.grid = [[ tile ]];
layer.initSingleTile(bounds); layer.initSingleTile(bounds);
@@ -507,13 +511,15 @@
layer.grid = []; layer.grid = [];
//more useful mocks //more useful mocks
layer.map = { layer.map = {
baseLayer: {wrapDateLine: false},
getMaxExtent: function() { return new OpenLayers.Bounds(-180,-90,180,90); },
getLayerPxFromLonLat: function(ul) { getLayerPxFromLonLat: function(ul) {
return { return {
x:ul.lon, x:ul.lon,
y:ul.lat y:ul.lat
}; };
}, },
getResolution:function(){return 1;} getResolution: function(){return 1;}
}; };
layer.addTile = function(tileBounds, px) { layer.addTile = function(tileBounds, px) {
t.ok(tileBounds.equals(desiredTileBounds), "correct tile bounds passed to addTile to create new tile"); t.ok(tileBounds.equals(desiredTileBounds), "correct tile bounds passed to addTile to create new tile");
@@ -541,6 +547,13 @@
translatedPX = {x:-40,y:145}; translatedPX = {x:-40,y:145};
layer.grid = [[ tile ]]; layer.grid = [[ tile ]];
layer.initSingleTile(bounds); layer.initSingleTile(bounds);
t.ok(layer.tileSize.equals(new OpenLayers.Size(80, 125)), "tileSize correct.");
//test bounds where ratio will be applied on all edges
layer.displayOutsideMaxExtent = true;
desiredTileBounds = new OpenLayers.Bounds(-40,-35,80,145);
layer.initSingleTile(bounds);
t.ok(layer.tileSize.equals(new OpenLayers.Size(120, 180)), "tileSize correct.")
} }
function test_Layer_Grid_addTileMonitoringHooks(t) { function test_Layer_Grid_addTileMonitoringHooks(t) {