If you want to know if a layer is on the map, then either check the map:

By Name -
if(map.findLayersByName(layerName).length){
   // we found the layer
} else {
  // layer not found
}
By Layer -
if(map.layers.indexOf(layer)>-1){
   // we found it
} else {
 // layer not in map
}

or check the layer store
By Name -
var index = mapPanel.layers.findBy(function(rec){
   return rec.getLayer().name == layerName // assumes that layerName has been 
previously defined
})
if(index>-1){ //found the layer } else { //no layer matched }
By Layer
if(mapPanel.layers.getByLayer(layer)){ //found the layer } else { //no layer 
found }

If you want to know if a node exists in the tree with then just cascade down 
the tree
var rootNode = layerTree.getRootNode();
var res = {layerFound:false};
// find by layer, useful for gx_layers loaded by a layerContainer; assume that 
layer has been previously defined   
rootNode.cascade(function(node){
   if(node.layer && node.layer == layer){
        this.layerFound = true;      
        return false;
   }
},res);

//find by text. assume searchText has already been defined
rootNode.cascade(function(node){
    if(node.text == searchText){
       this.layerFound = true;
       return false;
    }
},res);

if(res.layerFound){  //found the node } else {  //no matching node found }

Matt Priour
Kestrel Computer Consulting


From: IT Intern 
Sent: Tuesday, January 18, 2011 11:32 AM
To: Robert Buckley 
Cc: [email protected] 
Subject: Re: [Users] how to find a layer in layertree


hmm this might be a brute-force way of doing this, but I think writing an event 
handler for everytime a new layer is added (assuming by button click) you 
traverse the entire tree..

for(var c = 0; c < 
layerRoot.getOwnerTree().getNodeById(parent.id).childNodes.length; c++){
        
layerRoot.getOwnerTree().getNodeById(parent.id).childNodes[c].eachChild(function(node)
 {          
            if (node.layer.name == map.getLayersByName(layerName)) {  
               //then put logic in here...maybe create a count variable or 
something like
                      layerExists += 1;
            }//end if  
        });//end eachChild
    }//end for

Hope this gives you some kind of lead,

elshae


On Tue, Jan 18, 2011 at 12:09 PM, Robert Buckley <[email protected]> 
wrote:

  Hi,

  I am trying to write a function which checks to see if a layer is present in 
the tree.

  if the layer already exists in the tree (or map) is shouldn´t be loaded...


  can anyone help?


  var clonename = clone.get("layer").name
              
              if (mapPanel.layers(clonename)) {
                  Ext.MessageBox.alert(null, "test");
              }
              else 
              {
                  mapPanel.layers.add(clone);
                  Ext.MessageBox.alert(null, clonename);
              }

  Cheers
  Rob




  _______________________________________________
  Users mailing list
  [email protected]
  http://www.geoext.org/cgi-bin/mailman/listinfo/users






--------------------------------------------------------------------------------


_______________________________________________
Users mailing list
[email protected]
http://www.geoext.org/cgi-bin/mailman/listinfo/users
_______________________________________________
Users mailing list
[email protected]
http://www.geoext.org/cgi-bin/mailman/listinfo/users

Reply via email to