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>