I've modified this sample to pass in a parameter in the URL. To use it, your URL would look like http://yoursite/testing/URLTest.html?id=IllinoisWithout the parameter(?id=Illinois), it defaults to California.
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:esri="http://www.esri.com/2008/ags"
pageTitle="FeatureLayer and its definitionExpression">
<!--
Problem:
How to search a layer for certain features and display the results on a map?
Solution:
The easiest way is to use "FeatureLayer" and modifying the definitionExpression property
based on the user input.
This sample use FeatureLayer and simply updates its definitionExpression when
user clicks the "Search" button (or hits enter in the text box), the definitionExpression
is then updated on the FeatureLayer which will request its new features.
Note that the busy cursor is shown while the definitionExpression is being updated.
Tip 1: if you wanted to use a different symbol, specify a symbol or renderer on the feature layer.
Tip 2: if you want to add a toolTip, listen to the graphicAdd event and add the tooltip at that time.
-->
<s:layout>
<s:VerticalLayout gap="10"
horizontalAlign="center"
paddingBottom="20"
paddingLeft="20"
paddingRight="20"
paddingTop="20"/>
</s:layout>
<fx:Script>
<![CDATA[
import com.esri.ags.events.LayerEvent;
import mx.controls.Alert;
import mx.rpc.events.FaultEvent;
private function doSearch():void
{
// fLayer.layerDetails.displayField
fLayer.definitionExpression = "STATE_NAME like '" + qText.text + "'";
}
// the following four functions are 'just' error handling and showing/hiding the busy cursor
protected function fLayer_updateStartHandler(event:LayerEvent):void
{
this.cursorManager.setBusyCursor();
}
protected function fLayer_updateEndHandler(event:LayerEvent):void
{
if (event.fault)
{
trace("updateEnd: " + event.fault); // maybe a badly formatted query?
}
else if (event.updateSuccess == false)
{
trace(event.type + ": " + event.updateSuccess + " ... unexpected failure");
}
else // things seem OK
{
if (FeatureLayer(event.layer).numGraphics < 1)
{
Alert.show("Sorry, found no such features, please try something else");
}
}
this.cursorManager.removeBusyCursor();
}
protected function fLayer_faultHandler(event:FaultEvent):void
{
Alert.show(event.fault.faultString + "\n\n" + event.fault.faultDetail, "FeatureLayer Fault " + event.fault.faultCode);
}
protected function fLayer_loadErrorHandler(event:LayerEvent):void
{
Alert.show(event.fault.faultString + "\n\n" + event.fault.faultDetail, "FeatureLayer Load Error " + event.fault.faultCode);
}
private function getURLParameters():Object
{
var result:URLVariables = new URLVariables();
try
{
if (ExternalInterface.available)
{
var search:String = ExternalInterface.call("location.search.substring",1);
if (search && search.length > 0)
{
result.decode(search);
}
}
}
catch (error:Error)
{
Alert.show(error.toString());
}
return result;
}
private function loading_Event():void
{
var params:Object = getURLParameters();
if (params["id"])
{
qText.text = params.id;
}
else
{
qText.text = "California";
}
doSearch();
}
]]>
</fx:Script>
<s:Panel height="60"
backgroundColor="0xB2BFC6"
title="Query a layer (search for U.S. states)">
<s:layout>
<s:HorizontalLayout/>
</s:layout>
<s:TextInput id="qText"
width="100%"
enter="doSearch()"
text="California"
toolTip="You may use % as a wildcard, e.g., New%"/>
<s:Button click="doSearch()" label="Search"/>
</s:Panel>
<esri:Map id="myMap">
<esri:extent>
<esri:Extent xmin="-14305000" ymin="2748000" xmax="-6815000" ymax="7117000">
<esri:SpatialReference wkid="102100"/>
</esri:Extent>
</esri:extent>
<esri:ArcGISTiledMapServiceLayer url="http://server.arcgisonline.com/ArcGIS/rest/services/World_Physical_Map/MapServer"/>
<esri:FeatureLayer id="fLayer"
fault="fLayer_faultHandler(event)"
load="{loading_Event()}"
loadError="fLayer_loadErrorHandler(event)"
mode="snapshot"
updateEnd="fLayer_updateEndHandler(event)"
updateStart="fLayer_updateStartHandler(event)"
url="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/5"/>
</esri:Map>
</s:Application>