Select to view content in your preferred language

Feature Layer layer as a variable

2439
15
Jump to solution
11-20-2013 12:27 PM
ionarawilson1
Deactivated User
Depending on what is selected on a combobox, the layer of a feature layer should change. How can I use a variable to do that?
For example, that 0 as one layer in the feature layer would be replaced by a variable, but I am not sure how to do that! Thanks

var layernumber  = 0   var stewardship = new FeatureLayer("http://tfsgis-iisd01:6080/arcgis/rest/services/Stewardship_SA3/FeatureServer/0",      {           mode: FeatureLayer.MODE_SELECTION, id: 'stewardship',           outFields: ['*']         }); 
0 Kudos
15 Replies
ionarawilson1
Deactivated User
Now the error is:

activity.item is null
0 Kudos
ionarawilson1
Deactivated User
I put the code snippet inside the change function and it is not null anymore, I can see the value in the console, but since the feature layer is being created outside the change function, it does not update the layer. If I create the feature layer inside the change function, other functions will not be able to access it. How can I make it update the layer? thank you so much for your help!!!

dojo.connect(dijit.byId("Activity"), 'onChange', function(value){ 
             //alert('ok ' + event);
    activity = dijit.byId('Activity');
           var layerId = activity.item["layerId"]

            console.log(layerId)
   activityGroupValue = dijit.byId('ActivityGroup').get('value')
   
   if (activityGroupValue == " " && value in oc(forestrylist))
   {
   
    dijit.byId('ActivityGroup').set('value', "Forestry")
    
   }
   
   else if (activityGroupValue == " " && value in oc(medialist))
   {
   
    dijit.byId('ActivityGroup').set('value', "Media")
    
   }
   else if (activityGroupValue == " " && value in oc(conservationeducationlist))
   {
    
    dijit.byId('ActivityGroup').set('value', "Conservation Education")
    
   }
   
   else if  (activityGroupValue == " " && value in oc(firelist))
   {
   
    dijit.byId('ActivityGroup').set('value', "Fire")
    
   }
   
   
   else if (activityGroupValue == " " && value in oc(urbanforestrylist))
   {
   
    dijit.byId('ActivityGroup').set('value', "Urban Forestry")
    
   }


   else if  (activityGroupValue == " " && value in oc(professionaldevelopmentlist))
   {
   
    dijit.byId('ActivityGroup').set('value', "Professional Development")
    
   }
   
   else if (activityGroupValue == " " && value in oc(oakwiltlist))
   {
   
    dijit.byId('ActivityGroup').set('value', "Oak Wilt")
    
   }
   else if (value == "Forest Stewardship Plan") {
   
     x = 0
   }
 });
   
    
var stewardship = new FeatureLayer("http://tfsgis-iisd01:6080/arcgis/rest/services/FeatureServiceStewAAreaAPoint/FeatureServer/" + layerId, {
          mode: FeatureLayer.MODE_SELECTION, 
          id: 'stewardship_' + layerId,
          outFields: ['*']
});
  
   

   map.addLayers([stewardship]);

0 Kudos
ionarawilson1
Deactivated User
I created a global variable for stewardship, and also used that variable inside the change function. I can see in the console that the layer id changes inside the function, but the layer in the map does not change, because it is still looking at the global variable value. Any ideas on what to do ? Thank you!!
   
 var stewardship = new FeatureLayer("http://tfsgis-iisd01:6080/arcgis/rest/services/FeatureServiceStewAAreaAPoint/FeatureServer/0" , {
          mode: FeatureLayer.MODE_SELECTION, 
         
          outFields: ['*']
});
  
 map.addLayers([stewardship]);

   dojo.connect(dijit.byId("Activity"), 'onChange', function(value){ 
             //alert('ok ' + event);
    activity = dijit.byId('Activity');
           var layerId = activity.item["layerId"]

            console.log(layerId)
   activityGroupValue = dijit.byId('ActivityGroup').get('value')
   
   if (activityGroupValue == " " && value in oc(forestrylist))
   {
   
    dijit.byId('ActivityGroup').set('value', "Forestry")
    
   }
   
   else if (activityGroupValue == " " && value in oc(medialist))
   {
   
    dijit.byId('ActivityGroup').set('value', "Media")
    
   }
   else if (activityGroupValue == " " && value in oc(conservationeducationlist))
   {
    
    dijit.byId('ActivityGroup').set('value', "Conservation Education")
    
   }
   
   else if  (activityGroupValue == " " && value in oc(firelist))
   {
   
    dijit.byId('ActivityGroup').set('value', "Fire")
    
   }
   
   
   else if (activityGroupValue == " " && value in oc(urbanforestrylist))
   {
   
    dijit.byId('ActivityGroup').set('value', "Urban Forestry")
    
   }


   else if  (activityGroupValue == " " && value in oc(professionaldevelopmentlist))
   {
   
    dijit.byId('ActivityGroup').set('value', "Professional Development")
    
   }
   
   else if (activityGroupValue == " " && value in oc(oakwiltlist))
   {
   
    dijit.byId('ActivityGroup').set('value', "Oak Wilt")
    
   }
  
       
stewardship = new FeatureLayer("http://tfsgis-iisd01:6080/arcgis/rest/services/FeatureServiceStewAAreaAPoint/FeatureServer/" + layerId, {
          mode: FeatureLayer.MODE_SELECTION, 
          id: 'stewardship_' + layerId,
          outFields: ['*']
    
});
  
   

   map.addLayers([stewardship]);
   console.log(stewardship)

0 Kudos
JasonZou
Frequent Contributor
What you can do is to declare a module or global level variable, which will be a key-value hash object. Here is the concept-proof code.
var loadedFeatLayers = {};

dojo.connect(dijit.byId("Activity"), 'onChange', function(value){ 
    activity = dijit.byId('Activity');
    var layerId = activity.item["layerId"];    // item["layerId"] may be an array; if so, use item["layerId"][0]
    var layer = loadedFeatLayers[layerId];

    // if the feature layer not created, create it.
    if (!layer) {
        loadedFeatLayers[layerId] = new FeatureLayer("http://tfsgis-iisd01:6080/arcgis/rest/services/FeatureServiceStewAAreaAPoint/FeatureServer/" + layerId, {
          mode: FeatureLayer.MODE_SELECTION, 
          id: 'stewardship_' + layerId,
          outFields: ['*']
        });

        map.addLayer(loadedFeatLayers[layerId]);
    }
    // if loaded, make it visible if not visible
    else {
        if (!layer.visible) layer.setVisibility(true);
    }
});
0 Kudos
ionarawilson1
Deactivated User
I used your code and I can see the value changing on the console, but nothing happens on the application. I actually need the layer to change in the editor template (I am using the editor widget), but the editor is not adding any layers when using this method. Here is the snippet with your added code.
var loadedFeatLayers = {};
dojo.connect(dijit.byId("Activity"), 'onChange', function(value){ 
             //alert('ok ' + event);
    //remove the existing stewardship layer
  
      activity = dijit.byId('Activity');
   var layerId = activity.item["layerId"];    // item["layerId"] may be an array; if so, use item["layerId"][0]
   var layer = loadedFeatLayers[layerId];
  if (!layer) {
        loadedFeatLayers[layerId] = new FeatureLayer("http://tfsgis-iisd01:6080/arcgis/rest/services/FeatureServiceStewAAreaAPoint/FeatureServer/" + layerId, {
          mode: FeatureLayer.MODE_SELECTION, 
          id: 'stewardship_' + layerId,
          outFields: ['*']
        });
        alert("TEST1")
        map.addLayer(loadedFeatLayers[layerId]);
  console.log(loadedFeatLayers[layerId])
  }
    // if loaded, make it visible if not visible
     else {
  alert("TEST2")
        if (!layer.visible) {
   layer.setVisibility(true);
  }
    }


And here are the code snippets that are creating the editor:
map.on("layers-add-result", initEditor);
 function initEditor(evt) {
 
          var templateLayers = arrayUtils.map(evt.layers, function(result){
            return result.layer;
 

          });


 var templatePicker = new TemplatePicker({
            featureLayers: templateLayers,
            grouping: true,
            rows: "auto",
            columns: 3
          }, "templateDiv");
          templatePicker.startup();

          var layers = arrayUtils.map(evt.layers, function(result) {
     


 var settings = {
            map: map,
            templatePicker: templatePicker,
            layerInfos: layers,
            toolbarVisible: true,
            createOptions: {
              polylineDrawTools:[ Editor.CREATE_TOOL_FREEHAND_POLYLINE ],
              polygonDrawTools: [ Editor.CREATE_TOOL_FREEHAND_POLYGON,
                Editor.CREATE_TOOL_CIRCLE,
                Editor.CREATE_TOOL_TRIANGLE,
                Editor.CREATE_TOOL_RECTANGLE
              ]
            },
            toolbarOptions: {
              reshapeVisible: true,
     mergeVisible: true,
     cutVisible: true
     
     
     
            }
     
          };

          var params = {settings: settings};    
          var myEditor = new Editor(params,'editorDiv');
          //define snapping options
          var symbol = new SimpleMarkerSymbol(
            SimpleMarkerSymbol.STYLE_CROSS, 
            15, 
            new SimpleLineSymbol(
              SimpleLineSymbol.STYLE_SOLID, 
              new Color([255, 0, 0, 0.5]), 
              5
            ), 
            null
          );
          map.enableSnapping({
            snapPointSymbol: symbol,
            tolerance: 20,
            snapKey: keys.ALT
          });
0 Kudos
ionarawilson1
Deactivated User
The problem is that the layer is not being added to the map. When I click on the url on the console I can see this:

__proto__
Object { declaredClass="esri.layers.FeatureLayer", invalidParams="query contains one or more unsupported parameters", _eventMap={...}, more...}

Wouldn't be a problem with the id perhaps? Why do I make it equal to 'stewardship_' + layerId?

Here is a code snippet with part of the data so  you can see I am setting the layerId as number

items: [
            {name:"Forest Stewardship Plan", id:0, layerId:0},
   {name:"Non Forest Stewardship Plan", id:1, layerId:1},
            {name:"Reforestation Acres - TFS", id:2, layerId:2},
            {name:"Reforestation Acres - Non TFS", id:3, layerId:1},
            {name:"Reforestation Assist",  id:4, layerId:0},
            {name:"Tree Farm Program Inspection - Inspection",  id:5, layerId:1},
            {name:"Tree Farm Program Inspection - Reinspection", id:6, layerId:2},
            {name:"Certified Forest Steward Assist",  id:7, layerId:1},
            {name:"Timber Theft Damage Appraisal",  id:8, layerId:0},
            {name:"Referral - Consulting Forester",  id:9, layerId:1},
            {name:"Referral - Vendor",  id:10, layerId:2},
            {name:"Rural Forestry Incidental Assist",  id:13, layerId:1},
   {name:"Prevention and Reduction of Pest Losses",  id:12, layerId:0},
   {name:"Forest Health Monitoring",  id:13, layerId:1},
   {name:"FIA Assist",  id:14, layerId:2},
   {name:"BMP Assist",  id:15, layerId:1},
   {name:"OW On-Site Assist", id:16, layerId:2},
   {name:"OW Not On Site Assist", id:17, layerId:2},
   {name:"OW Incidental Assist", id:18, layerId:2},
   {name:"OW Presentation", id:19, layerId:2},
   {name:"OW Training Given", id:20, layerId:2},
   {name:"OW Training Received", id:21, layerId:2},

However, the items on this combobox get filtered based on another combobox . I noticed that if Forestry (first item in this combobox), gets selected first, then I don't see an error (but still dont see the layer appearing on the map or in the editor template), but I choose another selection, then I get

TypeError: activity.item is null
I am not sure why I see this error.

Here are the contents of the combobox that filters the other combobox, which is used to select the layer
      var stateStore = new Memory({
   
        data: [
            {name:"Forestry", id:"FO", layerId:1},
   {name:"Oak Wilt", id:"OW", layerId:1},
            {name:"Urban Forestry", id:"UF", layerId:1},
            {name:"Fire", id:"Fi", layerId:1},
            {name:"Conservation Education", id:"CE", layerId:1},
            {name:"Professional Development", id:"PD", layerId:1},
            {name:"Media", id:"ME", layerId:1}
         
        ]
    });
0 Kudos