[OpenLayers-Users] Changing map max extent

Fernando González fergonco at gmail.com
Mon Apr 25 05:05:13 EDT 2011


Hi, is it possible to change the maxExtent of the map dynamically? I
have this code[1] where I set a maxExtent containing the only layer.
When I hit the "add layer" button I add another layer outside that
maxExtent and then I change the map maxExtent attribute in order to
make it contain both layers. However, I cannot move the map outside
the original maxExtent.

I'm afraid is a bad usage of the library, but I haven't enough
experience to solve the problem. Any help will be greatly appreciated.

Regards.


[1]

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <link rel="stylesheet" href="../theme/default/style.css"
type="text/css" />
        <link rel="stylesheet" href="style.css" type="text/css" />
        <script src="war/ol/OpenLayers.js"></script>
        <script type="text/javascript">
            var map;
            function init(){
                map = new OpenLayers.Map('map', {
  					tileSize : new OpenLayers.Size(400, 400),
  					allOverlays : true,
  					maxResolution : 'auto',
  					maxExtent : new OpenLayers.Bounds(0, 0, 10, 10),
//  					maxExtent : new OpenLayers.Bounds(0, 0, 1010, 1010),
  					projection : 'EPSG:23030'
                });

                var boxes  = new OpenLayers.Layer.Vector( "WKT" );
                var feature = new OpenLayers.Feature.Vector(
OpenLayers.Geometry.fromWKT("POLYGON ((1 1, 9 1, 9 9, 1 9, 1 1))"));
                boxes.addFeatures(feature);

                map.addLayer(boxes);
                map.zoomToMaxExtent();
            }

            function addLayer() {
                var boxes  = new OpenLayers.Layer.Vector( "WKT2" );
                var feature = new OpenLayers.Feature.Vector(
OpenLayers.Geometry.
                	fromWKT("POLYGON ((1001 1001, 1009 1001, 1009 1009,
1001 1009, 1001 1001))"));
                boxes.addFeatures(feature);

                map.addLayer(boxes);

                map.maxExtent = new OpenLayers.Bounds(0, 0, 1010, 1010);
                map.zoomToMaxExtent();
                alert('done');
            }
        </script>
    </head>
    <body onload="init()">
    	<button onclick="addLayer()">add layer</button
        <div id="map" class="smallmap" style="height: 300px; width:
300px;"></div>
        <div id="docs"></div>
    </body>
</html>


More information about the Users mailing list