[OpenLayers-Users] Zoom on GML extent

Christopher Schmidt crschmidt at metacarta.com
Mon Jan 18 10:43:00 EST 2010


On Mon, Jan 18, 2010 at 01:32:24PM +0100, christophe triquet wrote:
> Hi,
> 
> I'm adding a GML file in my map with the object OpenLayers.Layer.GML. I'm
> trying to zoom to this layer extent at map's loading. I can do this if I
> know de bounding box coordinates with a postgis query.
> 
> My PHP code :
> 
>         var bounds = new OpenLayers.Bounds();
>         bounds.extend((new
> OpenLayers.LonLat(<?=$xmin?>,<?=$ymin?>)).transform(wgs84,
> map.getProjectionObject()));
>         bounds.extend((new
> OpenLayers.LonLat(<?=$xmax?>,<?=$ymax?>)).transform(wgs84,
> map.getProjectionObject()));
>         bounds.toBBOX();
>         map.zoomToExtent(bounds);
> 
> But this requires a server call and I want my map to use only datas in the
> GML file and not to have to connect to a database.
> 
> I have seen that GML files includes bounded box informations like :
> 
>   <gml:boundedBy>
>     <gml:Box>
> 
> <gml:coord><gml:X>1.473839004763629</gml:X><gml:Y>43.43941299999661</gml:Y></gml:coord>
> 
> <gml:coord><gml:X>1.647671004707115</gml:X><gml:Y>43.58098800002078</gml:Y></gml:coord>
>     </gml:Box>
>   </gml:boundedBy>
> 
> Is it possible the get the gml extent to be able to automaticaly zoom on it
> at loading ?

Formats have a 'keepData' option or something like that, which then stashes
the data on the .data property of the format. You could then use standard
DOM methods to parse the data.

Alternatively, you could just do:

 map.zoomToExtent(dataLayer.getDataExtent());

after the loadend event fires on the layer, which would iterate over the
features and get a bounding box.

Regards,
-- 
Christopher Schmidt
MetaCarta



More information about the Users mailing list