[OpenLayers-Users] Ho to zoom to the extents of a layer

Jakob Voss jakob.voss at gbv.de
Wed Nov 18 10:50:05 EST 2009


Hi,

I want to show a KML file as overlay on a map. Unfortunately all 
examples I found have hard coded coordinates for the extend which part 
of the map to show. My KML file is generated dynamically so I want to 
zoom the Map to the extent of the layer.

The KML file is loaded and shown:

   kml = new OpenLayers.Layer.GML("KML", "mykml.kml", {
     format: OpenLayers.Format.KML,
     formatOptions: {
         extractAttributes: true,
         extractStyles: false
     },
     projection: map.displayProjection
   });
   map.addLayer(kml);

But when i later try to zoom to the objects included in the KML, the map 
does not move as expected (yes, the KML has been loaded when I call this):

   map.zoomToExtent( kml.getExtent() );

The getExtent() method of OpenLayers.Layer does only return the current 
viewport but not the whole extend. The following does not works neither:

   map.zoomToExtent( kml.maxExtent );

I think the problem is that the KML layer may lie outside of the current 
viewport and so it does not get loaded (?) I just want to get the 
smallest bounding box that contains all coordinates in the KML file. How 
can you do this?

Thanks!
Jakob

-- 
Jakob Voß <jakob.voss at gbv.de>, skype: nichtich
Verbundzentrale des GBV (VZG) / Common Library Network
Platz der Goettinger Sieben 1, 37073 Göttingen, Germany
+49 (0)551 39-10242, http://www.gbv.de



More information about the Users mailing list