diff --git a/lib/OpenLayers/Layer/Grid.js b/lib/OpenLayers/Layer/Grid.js index f23ccee3a2..99d8814d52 100644 --- a/lib/OpenLayers/Layer/Grid.js +++ b/lib/OpenLayers/Layer/Grid.js @@ -718,19 +718,18 @@ OpenLayers.Layer.Grid = OpenLayers.Class(OpenLayers.Layer.HTTPRequest, { */ initSingleTile: function(bounds) { this.clearTileQueue(); - //determine if a maxExtent restriction exists - var maxExt = this.maxExtent || this.map.getMaxExtent() || this.map.maxExtent; - + //determine new tile bounds var tileWidth, tileHeight, tileBounds; var center = bounds.getCenterLonLat(); + //adjust tile bounds to fit in maxExtent restriction - //if it is an overlay and there is a maxExtent restriction - if(this != this.map.baseLayer && maxExt && bounds.containsBounds(maxExt)) { - bounds.bottom = Math.max(maxExt.bottom, bounds.bottom); - bounds.top = Math.min(maxExt.top, bounds.top); - bounds.left = Math.max(maxExt.left, bounds.left); - bounds.right = Math.min(maxExt.right, bounds.right); + //if there is a maxExtent restriction + if(this.maxExtent && bounds.containsBounds(this.maxExtent, true)) { + bounds.bottom = Math.max(this.maxExtent.bottom, bounds.bottom); + bounds.top = Math.min(this.maxExtent.top, bounds.top); + bounds.left = Math.max(this.maxExtent.left, bounds.left); + bounds.right = Math.min(this.maxExtent.right, bounds.right); tileWidth = bounds.getWidth(); tileHeight = bounds.getHeight(); tileBounds = bounds; @@ -742,10 +741,10 @@ OpenLayers.Layer.Grid = OpenLayers.Class(OpenLayers.Layer.HTTPRequest, { lon : tileBounds.right, lat : tileBounds.top }); - this.tileSize = { - h : Math.abs(trPx.y - blPx.y), - w : Math.abs(trPx.x - blPx.x) - }; + this.tileSize = new OpenLayers.Size( + Math.abs(trPx.x - blPx.x), + Math.abs(trPx.y - blPx.y) + ); this._resetTileSize = true; } else { @@ -760,6 +759,7 @@ OpenLayers.Layer.Grid = OpenLayers.Class(OpenLayers.Layer.HTTPRequest, { ); if(this._resetTileSize === true) { this.setTileSize(); + delete this._resetTileSize; } } var px = this.map.getLayerPxFromLonLat({ diff --git a/tests/Layer/Grid.html b/tests/Layer/Grid.html index 34abdea092..1fc02c8a7a 100644 --- a/tests/Layer/Grid.html +++ b/tests/Layer/Grid.html @@ -449,7 +449,7 @@ } function test_Layer_Grid_initSingleTile(t) { - t.plan( 11 ); + t.plan( 19 ); layer = new OpenLayers.Layer.Grid(name, url, params, { singleTile: true, @@ -501,8 +501,46 @@ } }; layer.grid = [[ tile ]]; - layer.initSingleTile(bounds); - + layer.initSingleTile(bounds); + + //test maxExtent restrictions + //reset grid + layer.grid = []; + //more useful mocks + layer.map = { + getLayerPxFromLonLat: function(ul) { + return { + x:ul.lon, + y:ul.lat + }; + }, + getResolution:function(){return 1;} + }; + layer.addTile = function(tileBounds, px) { + t.ok(tileBounds.equals(desiredTileBounds), "correct tile bounds passed to addTile to create new tile"); + t.ok(px.x == translatedPX.x && px.y == translatedPX.y, "correct tile px passed to addTile to create new tile"); + return newTile; + }; + tile = { + moveTo: function(tileBounds, px) { + t.ok(tileBounds.equals(desiredTileBounds), "correct tile bounds passed to tile.moveTo()"); + t.ok(px.x == translatedPX.x && px.y == translatedPX.y, "correct tile px passed to tile.moveTo()"); + } + }; + //test bound fully contains the maxExtent + //tile bounds -10,10,50,100 + layer.maxExtent = new OpenLayers.Bounds(0,20,40,90); + desiredTileBounds = new OpenLayers.Bounds(0,20,40,90); + translatedPX = {x:0,y:90}; + layer.initSingleTile(bounds); + + //test bound overlaps the maxExtent + bounds = new OpenLayers.Bounds(-10,10,50,100); + layer.maxExtent = new OpenLayers.Bounds(-30,20,40,110); + desiredTileBounds = new OpenLayers.Bounds(-10,20,40,100); + translatedPX = {x:-10,y:100}; + layer.grid = [[ tile ]]; + layer.initSingleTile(bounds); } function test_Layer_Grid_addTileMonitoringHooks(t) {