Adding ArcGISDynamicMapServiceLayer but working with FeatureLayers

972
2
06-30-2010 04:27 PM
JMcNeil
Occasional Contributor III
Maybe I missing the concept of FeatureLayers.....all I want to do is add a ArcGISDynamicMapServiceLayer (I want to add all the layers within the map/service in one shot) but I want to select a few of those layers and work with them as featureLayers. Is there a way to do this?

For example lets use the sample titled "feature layer with selection" example from the ArcGIS Resource Center - Javascript API. (Code is added below).

What if I want to add the entire Service not just the KSPetro layer.  In this case there is only one other layer: "Wells" but what if there was a bunch of layers in the service?  I don't want to add them all individually and I don't want them all to be FeatureLayers. 

Is there a way to add the entire service and set a few layers as FeatureLayers and work with them.   "http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Petroleum/KSPetro/MapServer"
Obviously, I don't want to add the entire service and then add the featurelayers, this will add some layers to the map twice.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  <meta http-equiv="X-UA-Compatible" content="IE=7" />
  <title>Layer in a map service - [ON-DEMAND]</title>
    <link rel="stylesheet" type="text/css" href="http://serverapi.arcgisonline.com/jsapi/arcgis/2.0/js/dojo/dijit/themes/tundra/tundra.css">
    <script type="text/javascript">djConfig = { parseOnLoad:true };</script>
    <script type="text/javascript" src="http://serverapi.arcgisonline.com/jsapi/arcgis/?v=2.0"></script>

    <script type="text/javascript">
      dojo.require("esri.map");
      dojo.require("esri.layers.FeatureLayer");

      var selectionToolbar, featureLayer;

      function init() {
        var initialExtent = new esri.geometry.Extent(-97.5328,37.4344,-97.2582,37.64041, new esri.SpatialReference({wkid:4326}) );
        var map = new esri.Map("map", { extent: esri.geometry.geographicToWebMercator(initialExtent), slider: true, nav: true });
        dojo.connect(map, "onLoad", initSelectToolbar);
        var baseMapLayer = new esri.layers.ArcGISTiledMapServiceLayer("http://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer");
        map.addLayer(baseMapLayer);

        var fieldsSelectionSymbol = new esri.symbol.SimpleFillSymbol().setColor(new dojo.Color([255,255,0,0.5]));
        fieldsSelectionSymbol.setOutline(new esri.symbol.SimpleLineSymbol("dashdot", new dojo.Color([255,0,0]), 2));
       
        var content = "<b>Status</b>: ${STATUS}" +
                      "<br><b>Cummulative Gas</b>: ${CUMM_GAS} MCF" +
                      "<br><b>Total Acres</b>: ${APPROXACRE}" +
                      "<br><b>Avg. Field Depth</b>: ${AVG_DEPTH} meters";
        var infoTemplate = new esri.InfoTemplate("${FIELD_NAME}", content);

        featureLayer = new esri.layers.FeatureLayer("http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Petroleum/KSPetro/MapServer/1",{
          mode: esri.layers.FeatureLayer.MODE_ONDEMAND,
          infoTemplate: infoTemplate,
          outFields: ["*"]
        });

      
        featureLayer.setDefinitionExpression("PROD_GAS='Yes'");
        featureLayer.setSelectionSymbol(fieldsSelectionSymbol);

        dojo.connect(featureLayer, "onSelectionComplete", sumGasProduction);

        dojo.connect(featureLayer, "onSelectionClear", function(features) {
          dojo.byId('messages').innerHTML = "<i>No Selected Fields</i>";
        });
        map.addLayer(featureLayer);
      }

      function initSelectToolbar(map) {
        selectionToolbar = new esri.toolbars.Draw(map);
        var selectQuery = new esri.tasks.Query();
        dojo.connect(selectionToolbar, "onDrawEnd", function(geometry) {
          selectionToolbar.deactivate();
          selectQuery.geometry = geometry;
          featureLayer.selectFeatures(selectQuery, esri.layers.FeatureLayer.SELECTION_NEW);
        });
      }

      function sumGasProduction(features) {
        var productionSum = 0;
        dojo.forEach(features, function(feature) {
          productionSum = productionSum + feature.attributes.CUMM_GAS;
        });
        dojo.byId('messages').innerHTML = "<b>Selected Fields Production: " + productionSum + " mcf. </b>";
      }

    dojo.addOnLoad(init);
  </script>

</head>

<body class="tundra">
  <button dojoType="dijit.form.Button" onClick="selectionToolbar.activate(esri.toolbars.Draw.EXTENT);">Select Fields</button>
  <button dojoType="dijit.form.Button" onClick="featureLayer.clearSelection();">Clear Selection</button><br>
  <div id="map" style="position: relative; width:700px; height:500px; border:1px solid #000;"></div>
  <span id="messages"></span>
</body>
</html> 
0 Kudos
2 Replies
derekswingley1
Frequent Contributor
I think you'll have to add each layer you want to query/make selections against as a feature layer. If these layers happen to be in the same map service you're using as a base map, use setVisibleLayers() to exclude them before you add your dynamic map service layer to your map.

http://help.arcgis.com/en/webapi/javascript/arcgis/help/jsapi/arcgisdynamicmapservicelayer.htm#setVi...
0 Kudos
JMcNeil
Occasional Contributor III
Swingley,

Thanks for the reply.  I was think the samething (Add each layer as a feature layer).  I guess I can add them one by one.  The real problem I having is in another post (One I post a few days ago). see: http://forums.arcgis.com/threads/7304-Select-feature-set-send-table-grid
Basically I was trying to solve this problem with a different approach. I don't want to add the layers a feature layers because I have a TOC function that was written by another user (earlier API version before FeatureSets) and I can't seem to figure out how to add featuresets to display in the TOC.  If I add them in as baselayers and turn them off with the setVisibleLayers() they will still be visible because the FeatureSet will be visible....hence the reason I didn't want to add them twice.  I just wanted to add the entire service (all the layers) and then somehow set a few to work as featuresets.

Thanks for the reply, I really appreciate it. 🙂
0 Kudos