[OpenLayers-Users] ZoomToExtent of KML File

Peter Robins openlayers at peterrobins.co.uk
Thu Dec 11 05:27:07 EST 2008


On Mon, Sep 15, 2008, Christopher Schmidt-2 wrote:
> I assume you create your layer, and then immediately call getDataExtent 
> on it, before the KML has loaded. Listen to the layer's loadend event, 
> and then call getDataExtent when it has loaded. 

I've been experimenting with this, and it looks to me like 'loadend' won't 
fire until the map has been centered. If I adapt one of the OL example files 
to look something like:

            map = new OpenLayers.Map('map');
            layer = new OpenLayers.Layer.WMS( "OpenLayers WMS",
                    "http://labs.metacarta.com/wms/vmap0", 
{layers: 'basic'} );
            map.addLayer(layer);
            vectorLayer = new OpenLayers.Layer.GML("KML", "file.kml", 
               {
                format: OpenLayers.Format.KML, 
                eventListeners: { 'loadend': kmlLoaded } } );
            map.addLayer(vectorLayer);
        }
        function kmlLoaded(){
            map.zoomToExtent(vectorLayer.getDataExtent());
        }

it doesn't display anything; kmlLoaded is never called.
If I add a dummy setCenter before creating vectorLayer:
           map.setCenter(new OpenLayers.LonLat(0, 0), 1);
then it works, but if the file is a large one or the lines are slow etc, there 
is a noticeable pause whilst it's being loaded, during which it displays a 
potentially completely different map to the one formed by DataExtent - very 
confusing to the user.

Is it meant to work like this? Seems like a catch-22: you can't load the file 
without setting the center, and you don't know where to set the center until 
you've loaded the file. Or am I missing something?



More information about the Users mailing list