<?xml version="1.0" encoding="utf-8"?>
<mx:Application
xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:esri="http://www.esri.com/2008/ags"
pageTitle="Example - adding a layerDefinition to a query">
<esri:QueryTask id="queryTask"
showBusyCursor="true"
url="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/5" />
<esri:Query id="query" returnGeometry="true" outSpatialReference="{map.spatialReference}">
<esri:outFields>
<mx:String>*</mx:String>
</esri:outFields>
</esri:Query>
<mx:Script>
<![CDATA[
private function execQuery():void
{
//dyn.layerDefinitions[5] number five is the states layer that we are using in the queryTask.
query.where = "UPPER(STATE_NAME) = '" + txtInput.text.toUpperCase() + "' AND " + dyn.layerDefinitions[5];
queryTask.execute(query);
}
]]>
</mx:Script>
<mx:HBox width="100%" height="28" backgroundAlpha="1" backgroundColor="#B6B7C0"
verticalAlign="middle" paddingLeft="10" paddingRight="10" borderStyle="solid" borderThickness="1">
<mx:Label text="Query with definition expression" color="#000000"/>
<mx:TextInput id="txtInput" text="Mississippi" enter="execQuery()" color="#000000"/>
<!-- Use Spacer to push Button control to the right. -->
<mx:Spacer width="100%"/>
<mx:Button label="Search" click="execQuery()" color="#000000"/>
</mx:HBox>
<esri:Map id="map">
<esri:extent>
<esri:Extent xmin="-150" ymin="5" xmax="-50" ymax="68">
<esri:SpatialReference wkid="4326"/>
</esri:Extent>
</esri:extent>
<esri:ArcGISTiledMapServiceLayer
url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"/>
<esri:ArcGISDynamicMapServiceLayer id="dyn"
url="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer">
<esri:layerDefinitions>
<!-- Layers in the order 0..x of the map service -->
<mx:Array>
<mx:String></mx:String> <!-- Census block points -->
<mx:String></mx:String> <!-- Census block groups -->
<mx:String></mx:String> <!-- Counties -->
<mx:String><![CDATA[ AGE_UNDER5 + AGE_5_17 > AGE_50_64 + AGE_65_UP ]]></mx:String> <!-- Coarse Counties -->
<mx:String><![CDATA[ AGE_UNDER5 + AGE_5_17 > AGE_50_64 + AGE_65_UP ]]></mx:String> <!-- Detailed Counties -->
<mx:String><![CDATA[ AGE_UNDER5 + AGE_5_17 > AGE_50_64 + AGE_65_UP ]]></mx:String> <!-- States -->
</mx:Array>
</esri:layerDefinitions>
</esri:ArcGISDynamicMapServiceLayer>
<esri:GraphicsLayer id="myGraphicsLayer" graphicProvider="{queryTask.executeLastResult.features}"/>
</esri:Map>
</mx:Application>
Davide,
Yes you can use existing layerDefinitions and just append them to your query as I demonstrate in this sample.<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:esri="http://www.esri.com/2008/ags" pageTitle="Example - adding a layerDefinition to a query"> <esri:QueryTask id="queryTask" showBusyCursor="true" url="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/5" /> <esri:Query id="query" returnGeometry="true" outSpatialReference="{map.spatialReference}"> <esri:outFields> <mx:String>*</mx:String> </esri:outFields> </esri:Query> <mx:Script> <![CDATA[ private function execQuery():void { //dyn.layerDefinitions[5] number five is the states layer that we are using in the queryTask. query.where = "UPPER(STATE_NAME) = '" + txtInput.text.toUpperCase() + "' AND " + dyn.layerDefinitions[5]; queryTask.execute(query); } ]]> </mx:Script> <mx:HBox width="100%" height="28" backgroundAlpha="1" backgroundColor="#B6B7C0" verticalAlign="middle" paddingLeft="10" paddingRight="10" borderStyle="solid" borderThickness="1"> <mx:Label text="Query with definition expression" color="#000000"/> <mx:TextInput id="txtInput" text="Mississippi" enter="execQuery()" color="#000000"/> <!-- Use Spacer to push Button control to the right. --> <mx:Spacer width="100%"/> <mx:Button label="Search" click="execQuery()" color="#000000"/> </mx:HBox> <esri:Map id="map"> <esri:extent> <esri:Extent xmin="-150" ymin="5" xmax="-50" ymax="68"> <esri:SpatialReference wkid="4326"/> </esri:Extent> </esri:extent> <esri:ArcGISTiledMapServiceLayer url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"/> <esri:ArcGISDynamicMapServiceLayer id="dyn" url="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer"> <esri:layerDefinitions> <!-- Layers in the order 0..x of the map service --> <mx:Array> <mx:String></mx:String> <!-- Census block points --> <mx:String></mx:String> <!-- Census block groups --> <mx:String></mx:String> <!-- Counties --> <mx:String><![CDATA[ AGE_UNDER5 + AGE_5_17 > AGE_50_64 + AGE_65_UP ]]></mx:String> <!-- Coarse Counties --> <mx:String><![CDATA[ AGE_UNDER5 + AGE_5_17 > AGE_50_64 + AGE_65_UP ]]></mx:String> <!-- Detailed Counties --> <mx:String><![CDATA[ AGE_UNDER5 + AGE_5_17 > AGE_50_64 + AGE_65_UP ]]></mx:String> <!-- States --> </mx:Array> </esri:layerDefinitions> </esri:ArcGISDynamicMapServiceLayer> <esri:GraphicsLayer id="myGraphicsLayer" graphicProvider="{queryTask.executeLastResult.features}"/> </esri:Map> </mx:Application>
private function queryFeaturesGraphical(geom:Geometry):void
{
var i:Number = cboLayerGraphical.selectedIndex;
var querySpatialRel:String = "esriSpatialRelIntersects";
queryLayer = configSearchGraphical.url;
queryGeom = geom;
queryFields = configSearchGraphical.fields;
queryTitleField = configSearchGraphical.titlefield;
queryLinkField = configSearchGraphical.linkfield;
if (queryLayer)
{
var queryTask:QueryTask = new QueryTask(queryLayer);
var query:Query = new Query();
query.geometry = queryGeom;
query.outFields = queryFields.split(",");
query.returnGeometry = true;
query.spatialRelationship = querySpatialRel;
query.outSpatialReference = map.spatialReference;
queryTask.execute(query, new AsyncResponder(onResult, onFault));
showMessage(loadingLabel, true);
showStateResults(null);
// on result
function onResult(featureSet:FeatureSet, token:Object = null):void
{
try
{
var recAC:ArrayCollection = createRecordData(featureSet,"");
addSharedData(widgetTitle, recAC);
wRepeater.dataProvider = recAC;
showMessage(selectionLabel + " " + featureSet.features.length, false);
//Davide
}
catch (error:Error)
{
showMessage(error.message, false);
}
}
//on fault
function onFault(info:Object, token:Object = null) : void
{
showMessage(info.toString(), false);
}
}
}
//Loop though the map layers to find one that matches the searchwidgets selected layer
for each(var layer:* in map.layers)
{
//Check to see if this particular map service layer has the same url as
//the one selected by the searchWidget, minus the layer designator
//If the urls match then use the layer designator to get the appropriate
//layerDefinition and add it to the querys where clause
if(layer is ArcGISDynamicMapServiceLayer || layer is ArcGISTiledMapServiceLayer)
if(layer.url == queryLayer.substring(0,queryLayer.lastIndexOf("/"))){
if(layer.layerDefinitions)
{
query.where = layer.layerDefinitions[parseInt(queryLayer.substring(queryLayer.lastIndexOf("/")+ 1))];
trace(query.where);
}
}
}
Davide,
OK, helps to be specific. Add this//Loop though the map layers to find one that matches the searchwidgets selected layer for each(var layer:* in map.layers) { //Check to see if this particular map service layer has the same url as //the one selected by the searchWidget, minus the layer designator //If the urls match then use the layer designator to get the appropriate //layerDefinition and add it to the querys where clause if(layer is ArcGISDynamicMapServiceLayer || layer is ArcGISTiledMapServiceLayer) if(layer.url == queryLayer.substring(0,queryLayer.lastIndexOf("/"))){ if(layer.layerDefinitions) { query.where = layer.layerDefinitions[parseInt(queryLayer.substring(queryLayer.lastIndexOf("/")+ 1))]; trace(query.where); } } }