[OpenLayers-Users] How to restrict map extent?

shymon szymon.bogacz at o2.pl
Wed Apr 1 06:57:54 EDT 2009


Hello,

I'm trying to set up my own map app using OSM data, mapnik + PostGIS to render map, mod_tile to serve tiles and OpenLayers for map display. Everything works fine, except I cannot restrict viewable map area in OpenLayers. I want user to see only area of one city, and not to zoom out outside it.

So I have downloaded OSM XML file with map of requested area, loaded it to PostGIS with command like this:
osm2pgsql -d some-db -p some-prefix -U some-user -H localhost /neuromap/trunk/data/wroclaw-map.osm -c -E 900913 -b 16.8188,50.9974,17.2235,51.2065

(BTW: data is loaded to PostGIS but osm2pgsql says: 
StartElement: Unknown element name: bounds
EndElement: Unknown element name: bounds - maybe this is a problem?)

I create OpenLayers map as follows:

var mapnikLayer = new OpenLayers.Layer.OSM("world", "http://some-url...");
var mapOptions = {
    controls:[
        new OpenLayers.Control.PanZoomBar(),
        new OpenLayers.Control.Permalink(),
        new OpenLayers.Control.ScaleLine(),
        new OpenLayers.Control.OverviewMap(),
        new OpenLayers.Control.Permalink('permalink'),
        new OpenLayers.Control.MousePosition(),
        new OpenLayers.Control.KeyboardDefaults(),
        new OpenLayers.Control.MouseDefaults()
    ],
    maxExtent: new OpenLayers.Bounds(1863611,6614478,1924760,6660340),
    maxResolution: 0.6,
    units: 'm',
    //numZoomLevels: 19,
    projection: new OpenLayers.Projection("EPSG:900913")
  }  
  
  var map = new OpenLayers.Map ("wroc-map", mapOptions);
  map.addLayer(mapnikLayer);
  ...

When I call map.zoomToMaxExtent(), OpenLayers shows what I expect to be whole world area - not my city. When I display map.getMaxExtent() i get:

max extent: left-bottom=(-20037508.34,-20037508.34) right-top=(20037508.34,20037508.34)

so maxExtent is evidently not set.


I can do of course:
map.zoomToExtent(new OpenLayers.Bounds(1863611,6614478,1924760,6660340))
but that's not what I want because zoom level is set to 11. I would prefer to have zl=0 with the view of whole city.


I also tried to use scales:[220000,150000,100000,50000,20000,10000,5000,2000,1000] instead of maxExtent and maxResolution, but though I call zoomToExtent... and viewport has this extent I can see only placeholder tile images with message "...more OSM data soon". 


Is restricting of viewable map area possible?
What am I doing wrong?
Please help.

Shymon


-- 
View this message in context: http://n2.nabble.com/How-to-restrict-map-extent--tp2568378p2568378.html
Sent from the OpenLayers Users mailing list archive at Nabble.com.




More information about the Users mailing list