dynamically grabbing layers from a FeatureService

591
3
01-07-2011 02:20 PM
timgogl
New Contributor II
is it possible to dynamically grab the layers from a feature service?

currently i have the layers hardcoded in my application... i have been tinkering some,
and can do this:
 var tmp = "http://myserver/ArcGIS/rest/services/webedit/FeatureServer/";
 var max = 9;
 for(var i=0;i<max;i++){
  var fl = new esri.layers.FeatureLayer(tmp+i,{
   mode: esri.layers.FeatureLayer.MODE_ONDEMAND,
   outFields:["*"]
  });
  editLayers.push(fl);
 }



however this is not ideal, because i sitll must go out to look at the service to see what to set the max to....

i would love to do something like:
 //var tmp = "http://myserver/ArcGIS/rest/services/webedit/FeatureServer/";
        var tmp = new esri.layers.featurelayer(myservice);
 var max = tmp.layers.count;

 for(var i=0;i<max;i++){
  var fl = new esri.layers.FeatureLayer(tmp+i,{
   mode: esri.layers.FeatureLayer.MODE_ONDEMAND,
   outFields:["*"]
  });
  editLayers.push(fl);
 }


this way, the gis guys can go about changing the service how they want.. and i wont have to make any changes to my application.... heh. the gis team only get my services for a short time, then im off to other projects.
0 Kudos
3 Replies
KellyHutchins
Esri Frequent Contributor
One option might be to do something like this:

<script type="text/javascript">
    dojo.require('esri.layers.FeatureLayer');
      var baseURL;
      var map;
      function init() {
      var initExtent = new esri.geometry.Extent({"xmin":-8590965,"ymin":4695877,"xmax":-8553434,"ymax":4711164,"spatialReference":{"wkid":102100}});
      map = new esri.Map("map",{extent:initExtent});
      var basemap = new esri.layers.ArcGISTiledMapServiceLayer("http://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer");
      map.addLayer(basemap);  
      
      baseURL = "http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/HomelandSecurity/operations/FeatureServer";
        esri.request({
          url:baseURL, 
          content:{f:"json"},
          callbackParamName:"callback",
          load:addLayers, 
          error:esriConfig.defaults.io.errorHandler
        });
      }
      
    function addLayers(response,args){

     dojo.forEach(response.layers,function(layer){
      var featureLayer = new esri.layers.FeatureLayer(baseURL + "/" + layer.id,{
          mode: esri.layers.FeatureLayer.MODE_ONDEMAND,
          outFields: ["*"]
      });
      map.addLayer(featureLayer);

     });
    }
   
      dojo.addOnLoad(init);
    </script>

0 Kudos
timgogl
New Contributor II
THANK YOU!

heh. that is EXACTLY what i was looking for.
0 Kudos
timgogl
New Contributor II
yes. with one minor tweak it worked perfectly.
thanks again kelly!
0 Kudos