Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I have a custom map which I get from a mbtiles file with the bounds -13,25,49,60 and zoom from 4 to 10.The display div is dynamic, I mean that it depends on window size and the extent restriction works but still tries to load images around the map if the display div is bigger.
How can I limit the zooming out below 4? Here is my code:

var map;
var fromProjection = new OpenLayers.Projection("EPSG:4326");   // Transform from WGS 1984
var toProjection   = new OpenLayers.Projection("EPSG:900913"); // to Spherical Mercator Projection
var extent= new OpenLayers.Bounds(-13,25,49,60).transform( fromProjection, toProjection);
var mbTiles = new OpenLayers.Layer.TMS("base", "base.php", {
    getURL: mbtilesURL,
    type:'png',
    transitionEffect: "resize",
    isBaseLayer: true,

});
function mbtilesURL (bounds) {
    var db = "img/map/base.mbtiles";
    var res = this.map.getResolution();
    var x = Math.round ((bounds.left - this.maxExtent.left) / (res * this.tileSize.w));
    var y = Math.round ((this.maxExtent.top - bounds.top) / (res * this.tileSize.h));
    var z = this.map.getZoom();
    return this.url+"?db="+db+"&z="+z+"&x="+x+"&y="+((1 << z) - y - 1);
}
map = new OpenLayers.Map('map', {
    projection: new OpenLayers.Projection("EPSG:900913"),
    displayProjection: new OpenLayers.Projection("EPSG:4326"),
    layers: [mbTiles],
    restrictedExtent : extent,
    numZoomLevels: 11,
    controls: [
        new OpenLayers.Control.Navigation({
            dragPanOptions: {
                enableKinetic: true
            }
        }),
    new OpenLayers.Control.PanZoom(),
    new OpenLayers.Control.Attribution()
    ]
});
var position = new OpenLayers.LonLat(9.4679,37.6831).transform( fromProjection, toProjection);
var zoom = 4;
map.setCenter(position, zoom );
map.zoomToMaxExtent();
var switcherControl = new OpenLayers.Control.LayerSwitcher();
map.addControl(switcherControl);
switcherControl.maximizeControl();

I made it work! Had to add :

   resolutions: [4891.969809,2445.984905,1222.992452,611.4962262,305.7481131,152.8740565]

and to add to z+4 to get to right zoom level. I used this site http://simonmercier.net/blog/?p=88 to get the right resolutions to use.

share|improve this question
    
There are multiple ways to do this, as shown in the source of openlayers.org/dev/examples/zoomLevels.html –  BradHards Jun 2 '13 at 4:44
    
I have tried that example but I couldn't get it to work. –  Darkwander Jun 2 '13 at 12:08
    
Please convert your answer (added to your question above) as the answer (below). –  BradHards Jun 2 '13 at 21:01
    
BTW: That example shows the resolution list approach... –  BradHards Jun 2 '13 at 21:02

1 Answer 1

up vote 0 down vote accepted

I made it work! Had to add :

resolutions: [4891.969809,2445.984905,1222.992452,611.4962262,305.7481131,152.8740565]

and to add to z+4 to get to right zoom level.

I used this site http://simonmercier.net/blog/?p=88 to get the right resolutions to use.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.