[OpenLayers-Dev] Set map extent to extent of a layer

Tim Schaub tschaub at opengeo.org
Mon Oct 26 19:08:09 EDT 2009


Hey-

Clark Woodward wrote:
> I am having trouble setting the extent of my map to the extent of a 
> layer. The code below adds a Google base map and a WKT layer to the map 
> and correctly projects the polygon.
> 
> However, I need to set the map center and scale so that the layer is 
> correctly positioned on the map. Essentially I want to set the map 
> extent = layer extent.
> 
>  
> 
> I believe I am using the two lines below incorrectly
> 
>  
> 
> /var bounds = new OpenLayers.Bounds(thrvec.getExtent);/

getExtent is a function
getExtent() is the results of the function called with no arguments.

> 
> /alert (bounds.toString);/

toString is a function
toString() is the results of the function called with no arguments.

Does that make sense?

> 
>  
> 
> Can someone correct this code or provide me an alternate solution. 
> Thanks in advance!
> 
>  
> 
> Clark
> 
>  
> 
>  
> 
> Extent.js
> 
> ______________________________________________________________
> 
>  
> 
>  
> 
> function init() {
> 
>  
> 
>   var proj = new OpenLayers.Projection("EPSG:900913");
> 
>   var display = new OpenLayers.Projection("EPSG:4326");
> 
>   var map = new OpenLayers.Map("map", {projection: proj, 
> displayProjection: display});
> 
>   map.addControl(new OpenLayers.Control.MousePosition());
> 
>   var wkt = new OpenLayers.Format.WKT({'internalProjection': proj, 
> 'externalProjection': display});
> 
>  
> 
>   var gphy=new OpenLayers.Layer.Google("Google", {type: G_HYBRID_MAP, 
> numZoomLevels: 20, 'sphericalMercator': true, 'maxExtent': new 
> OpenLayers.Bounds(-20037508.34,-20037508.34,20037508.34,20037508.34)});
> 
>   gphy.setIsBaseLayer(true);
> 
>   map.addLayers([gphy]);
> 
>  
> 
> // WKT
> 
>  
> 
>   str='POLYGON((-118.460185 34.095586,-118.458857 34.095874,-118.457521 
> 34.095991,-118.456222 34.095947,-118.455012 34.095732,-118.453946 
> 34.095366,-118.453056 34.094858,-118.452386 34.094218,-118.451945 
> 34.093481,-118.451766 34.092677,-118.451837 34.091830,-118.452179 
> 34.090969,-118.452769 34.090148,-118.453576 34.089374,-118.454575 
> 34.088685,-118.455735 34.088115,-118.457001 34.087681,-118.458329 
> 34.087392,-118.459664 34.087275,-118.460964 34.087320,-118.462163 
> 34.087525,-118.463239 34.087900,-118.464118   34.088408,-118.464799 
> 34.089048,-118.465240 34.089785,-118.465420 34.090589,-118.465338 
> 34.091436,-118.464996 34.092288,-118.464417 34.093118,-118.463610 
> 34.093892,-118.462600 34.094572,-118.461451 34.095152,-118.460185 
> 34.095586))';
> 
>  
> 
>   features=wkt.read([str]);          
> 
>   var style1 ={fillColor: '#FFFF33', fillOpacity: .5};
> 
>   var thrvec=new OpenLayers.Layer.Vector('WKT',  {style: style1});
> 
>   map.addLayers([thrvec]);
> 
>   thrvec.addFeatures(features);
> 
>  
> 
>   var bounds = new OpenLayers.Bounds(thrvec.getExtent);
> 
>  
> 
>   alert (bounds.toString);
> 
>  
> 
>   bounds.transform(display,proj);
> 
>   map.zoomToExtent(bounds)
> 
>  
> 
>   var point = new OpenLayers.LonLat(-118.458591, 34.091633);
> 
>   point.transform(display,proj);
> 
>   map.setCenter(point,13);
> 
>  
> 
> }
> 
>  
> 
>  
> 
>  
> 
>  
> 
>  
> 
> HTML
> 
> ______________________________________________________________
> 
>  
> 
> <html xmlns="http://www.w3.org/1999/xhtml">
> 
>   <head>
> 
>     <script 
> src='http://maps.google.com/maps?file=api&amp;v=2&amp;key=ABQIAAAAjpkAC9ePGem0lIq5XcMiuhR_wWLPFku8Ix9i2SXYRVK3e45q1BQUd_beF8dtzKET_EteAjPdGDwqpQ'></script>
> 
>     <script src="OpenLayers.js"></script>
> 
>     <script src="extent.js"></script>
> 
>     <link rel="stylesheet" href="style.css" type="text/css" />
> 
>  
> 
>  
> 
>   </head>
> 
>   <body onload="init()">
> 
>     <div id="map" style="width: 671px; height: 461px; vertical-align: 
> middle; horizontal-align: center;"></div>
> 
>   </body>
> 
> </html>
> 
>  
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Dev mailing list
> Dev at openlayers.org
> http://openlayers.org/mailman/listinfo/dev


-- 
Tim Schaub
OpenGeo - http://opengeo.org
Expert service straight from the developers.



More information about the Dev mailing list