[OpenLayers-Users] Understanding events

Yves Moisan yves.moisan at boreal-is.com
Tue Dec 8 14:03:52 EST 2009


> Can you provide the code where the layer is created and added to the 
> map?  
Hi Scott,

There isn't much more to it other than :

objOptions = {};
objOptions['projection'] = new OpenLayers.Projection("EPSG:900913");
objOptions['isBaseLayer'] =false;

objOptions["protocol"] = new mapfish.Protocol.MapFish({
            url: mapfish.Util.formatURL(strURL),
            params: {format: "GeoJSON"},
            format: new OpenLayers.Format.GeoJSON()
        });

objOptions["styleMap"] =  ...

strLayerName = "SomeName";

objOLLayer = new OpenLayers.Layer.Vector(strLayerName, objOptions );
oMap.addLayer(objOLLayer);


> I think from there, I can help show you how to add a listener to the layer to call a method when it's done loading.


I thought 

MyMap.events.register("loadend", layers[0], selectFeature(layers[0],oFeatures[0].fid));

was effectively adding the selectFeature function as a listener to the loadend event ?

Maybe one thing that makes things more complex is that the JS is
generated from an XSLT template.  Here's the generated code :

   function zoomInitialViewCallback(response)
              {

                  var oFormat = new OpenLayers.Format.GeoJSON();
                  var oFeatures = oFormat.read(response.responseText);
                  var layers = objGPWidgetDefaultMap.getLayersByResource("Resource_Dumping_Site","featureserver");
               
                  //alert("Highlight on")

                    function selectFeature(layer, fid)
                    { 
                            for(var i = 0; i<layer.features.length;++i)
                                  {         
                                        if (layer.features[i].fid == fid)
                                        {         
                                            FeatureSelectControl.select(layer.features[i]);
                                            break;             
                                        }
                                  }
                    }


                    FeatureSelectControl = new OpenLayers.Control.SelectFeature( layers[0], {}); 
                    objGPWidgetDefaultMap.addControl(FeatureSelectControl);
                    FeatureSelectControl.activate();

                 

                  if(oFeatures && oFeatures.length > 0)                  

                  {
                      var bounds = oFeatures[0].geometry.getBounds();
                      

                    

                        selectFeature(layers[0],oFeatures[0].fid);
 
                      for(var i = 1; i<oFeatures.length;++i)
                      {         
                            bounds.extend(oFeatures[i].geometry.getBounds());
                            selectFeature(layers[0],oFeatures[i].fid);
                      }                       
                  
 ...				  
					  	  
                  }

                  if(!objGPWidgetDefaultMap.getCenter())
                  {
                      objGPWidgetDefaultMap.zoomToMaxExtent();
                  }
         } // end zoomInitialViewCallback


         function zoomInitialViewOnFeature(nFeatureId)
              {
                  objParams = {};
                  objOptions = {};
                  var oURLParams = {
                    'osmresource': 'Resource_Dumping_Site',
                    'osmservice': 'FeatureServer',
                    'random': Math.random()
                  };

                     
                    var strURL = '/geoprisma/samples/complex_workspace/proxy.php/dumping_site';
                    oURLParams.queryable = 'dumpingid'; 
                    oURLParams.dumpingid = nFeatureId;                   
                     
                   
                  var strParams = OpenLayers.Util.getParameterString(oURLParams);
                  if(strParams.length > 0) {
                      var strSeparator = (strURL.indexOf('?') > -1) ? '&' : '?';
                      strURL += strSeparator + strParams;
                  }
                  OpenLayers.loadURL(strURL, '', null, zoomInitialViewCallback);
         } //end zoomInitialViewOnFeature

              zoomInitialViewOnFeature("1");

So it's the zoomInitialViewOnFeature that is called which then calls
zoomInitialViewCallback.  Should the listener be defined on
zoomInitialViewCallback from within zoomInitialViewOnFeature ?

TIA,

Yves

> 
> Yves Moisan wrote:
> > Hi List,
> > 
> > I'm trying to wrap my head around OpenLayers events.  Here's code that
> > works if I uncomment the first line (alert) :
> > 
> > // alert("Highlight on")
> > 
> >     function selectFeature(layer, fid)
> >     { 
> >             for(var i = 0; i<DEFANGED_layer.features.length;++i)
> >                   {         
> >                         if (layer.features[i].fid == fid)
> >                         {         
> >                             FeatureSelectControl.select(layer.features[i]);
> >                             break;             
> >                         }
> >                   }
> >     }
> > 
> >     FeatureSelectControl = new OpenLayers.Control.SelectFeature( layers[0], {}); 
> >     MyMap.addControl(FeatureSelectControl);
> > 
> >     FeatureSelectControl.activate();
> > 
> >     if(oFeatures && oFeatures.length > 0)                  
> > 
> >         {
> >           var bounds = oFeatures[0].geometry.getBounds();
> >           
> >         // next line works if alert is "on"
> >         // selectFeature(layers[0],oFeatures[0].fid);
> > 
> >         // how do I register an event to wait until layer ends loading ?  Tried variations of the following commands :
> > 
> >         // MyMap.events.register("zoomend", layers[0], selectFeature(layers[0],oFeatures[0].fid));
> >         // layer.events.register("loadend", layers[0], selectFeature(layers[0],oFeatures[0].fid));
> > 
> >     ...
> > 
> > Problem is if I don't leave enough time for the layer to load, my
> > selectFeature function doesn't work.  
> > 
> > I've seen there are different event types on different OL objects, so I
> > tried registering events on the map and the layer objects (e.g. above),
> > but obviously I do not understand how the event mechanism works.  Any
> > tutorial on events that could help me understand events ?
> > 
> > TIA,
> > 
> > Yves
> > 
> > _______________________________________________
> > Users mailing list
> > Users at openlayers.org
> > http://openlayers.org/mailman/listinfo/users




More information about the Users mailing list