[OpenLayers-Users] Zooming to center of a KML layer after loading it - request for assistance

Andres Deparis andres.deparis at gmail.com
Thu Jul 7 02:30:45 EDT 2011


Hello everyone,

I am trying to be able to zoom or Pan (zooming first) to the center of the
data from my kml file that I load periodically. But, in my case, I can load
the data (which consist of one point at this moment) and then of course to
be able to center to that point. I have searched and found out that perhaps
my method does not work because I am zooming into the data but not waiting
for the data to be fully loaded.

Below is my piece of code. Thank you in advance for the advice.

Andres D.P

var map;

function init() {


       map = new OpenLayers.Map({
        div: "map",
        //projection: new OpenLayers.Projection("EPSG:900913"),
        units: "m",
        maxResolution: 156543.0339,
        maxExtent: new OpenLayers.Bounds(
                 119.715815, -0.771406, 97.637881  ,7.640186
        )
,panDuration: 250    });

 styleMap = new OpenLayers.StyleMap({'default':{
                        //label : "${th}",
                        label : "MYdog",
                        fontColor: "${fontColor}",
                        fontSize: "16px",
                        labelAlign: "${labelAlign}",
                        pointRadius: 2,
                        labelXOffset : 0,
                        labelYOffset : 20
                    }});

        var osm = new OpenLayers.Layer.OSM();
        //var vlayer = new OpenLayers.Layer.Vector( "Editable" );
        var MyKML = new OpenLayers.Layer.GML("KML", "dog.kml",
               {
                projection: new OpenLayers.Projection("EPSG:4326"),
                //displayProjection: new
OpenLayers.Projection("EPSG:900913"),

                format: OpenLayers.Format.KML,
                formatOptions: {
                  extractStyles: true,
                  extractAttributes: true,
                  maxDepth: 1
                },
                styleMap: styleMap
               });

   map.addLayers([osm, MyKML]);
   map.addControl(new OpenLayers.Control.PanZoomBar());
   map.addControl(new OpenLayers.Control.Navigation({dragPanOptions:
{enableKinetic: true}}));
   map.addControl(new OpenLayers.Control.LayerSwitcher());

//    map.addControl(new OpenLayers.Control.MouseToolbar());
//    map.addControl(new OpenLayers.Control.Navigation());
//    map.addControl(new OpenLayers.Control.LayerSwitcher());
//    map.addControl(new OpenLayers.Control.PanZoom());
//    map.addControl(new OpenLayers.Control.EditingToolbar();
//    map.addControl(OpenLayers.Control.EditingToolbar(vlayer));


    map.setCenter(new OpenLayers.LonLat(101.587411,
3.021547).transform(new OpenLayers.Projection ("EPSG:4326"),
map.getProjectionObject()
),
15);


//map.zoomToExtent( MyKML.GetExtent() );

window.setInterval(raingaugeRefreshData, 10000, MyKML);


}

function raingaugeRefreshData(layer)
{
//setting loaded to false unloads the layer//


                layer.loaded = false;
                //setting visibility to true forces a reload of the layer//
                layer.setVisibility(true);
                //the refresh will force it to get the new KML data//
                layer.setUrl('MyDOG.kml?_salt='+Math.random());
                //- <3 from Thqr -//
         map.zoomToExtent( MyKML.maxExtent );



}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/openlayers-users/attachments/20110707/55062fd8/attachment.html


More information about the Users mailing list