FeatureLayer needs access to LayerInfos

1858
2
Jump to solution
11-08-2010 06:31 PM
StephenLead
Regular Contributor III
Given a REST endpoint, how can I programatically add all of the component layers to the map as FeatureLayers?

The sample at http://help.arcgis.com/EN/webapi/javascript/arcgis/help/jssamples_start.htm#jssamples/map_dynamiclay... shows how to use LayerInfos to add all of the layers in the REST endpoint to the map, using setVisibleLayers in a DynamicMapService.

But I can't see a method to do the same thing using Feature Layers. How can I programatically traverse all of the layers in a Map Server REST endpoint, and add these to the map as Feature Layers?

Thanks,
Steve
0 Kudos
1 Solution

Accepted Solutions
KellyHutchins
Esri Frequent Contributor
One approach would be to use esri.request then add each layer to the map:

    dojo.require('esri.layers.FeatureLayer');
      var baseURL;
      var map;
      function init() {
      var initExtent = new esri.geometry.Extent({"xmin":-8590965.388453046,"ymin":4695877.530747742,"xmax":-8553434.807565076,"ymax":4711164.936404756,"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/MapServer";
        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);

View solution in original post

0 Kudos
2 Replies
KellyHutchins
Esri Frequent Contributor
One approach would be to use esri.request then add each layer to the map:

    dojo.require('esri.layers.FeatureLayer');
      var baseURL;
      var map;
      function init() {
      var initExtent = new esri.geometry.Extent({"xmin":-8590965.388453046,"ymin":4695877.530747742,"xmax":-8553434.807565076,"ymax":4711164.936404756,"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/MapServer";
        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);
0 Kudos
StephenLead
Regular Contributor III
Awesome, thanks once again Kelly
0 Kudos