Hello Everyone,I am attempting to validate if a user clicks within a polygon. I have created event listeners for the query but they are not firing at all.I am using the Portland ESRI Landbase AGO Mapservice. Can someone take a look at my code and let me know where I am going wrong?
<?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"
xmlns:mx="library://ns.adobe.com/flex/halo"
pageTitle="Test query if user clicks within a polygon">
<fx:Declarations>
</fx:Declarations>
<fx:Script>
<![CDATA[
import com.esri.ags.FeatureSet;
import com.esri.ags.Graphic;
import com.esri.ags.SpatialReference;
import com.esri.ags.events.MapEvent;
import com.esri.ags.events.MapMouseEvent;
import com.esri.ags.geometry.MapPoint;
import com.esri.ags.layers.TiledMapServiceLayer;
import com.esri.ags.tasks.QueryTask;
import com.esri.ags.tasks.supportClasses.Query;
import com.esri.ags.utils.WebMercatorUtil;
import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.events.FlexEvent;
import mx.rpc.AsyncResponder;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.rpc.http.HTTPService;
public function onMapClick(event:MapMouseEvent):void
{
const mapPoint:MapPoint = myMap.toMapFromStage(event.stageX, event.stageY);
var latNumber:Number = int(mapPoint.y.toFixed(6));
var longNumber:Number = int(mapPoint.x.toFixed(6));
var latString:String = mapPoint.y.toFixed(6);
var longString:String = mapPoint.x.toFixed(6);
latitudeText.text=latString; // "Show the latitude clicked"
longitudeText.text=longString; // "Show the longitude clicked"
var wgs:SpatialReference = new SpatialReference(4326);
var myNewMapPoint:MapPoint = new MapPoint(latNumber,longNumber,wgs);
var queryTask:QueryTask = new QueryTask();
queryTask.showBusyCursor = true;
queryTask.useAMF = false;
queryTask.url = "http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Portland/Portland_ESRI_LandBase_AGO/MapServer/4";
var myQuery:Query = new Query();
myQuery.geometry = myNewMapPoint;
myQuery.spatialRelationship = "esriSpatialRelWithin";
myQuery.returnGeometry = true;
queryTask.addEventListener(ResultEvent.RESULT, onResult);
queryTask.addEventListener(FaultEvent.FAULT, onFault);
// Did the user click within a polygon
function onResult(event:ResultEvent):void
{
Alert("You clicked inside the polygon");
}
function onFault(event:FaultEvent):void
{
Alert("You clicked outside the polygon");
}
}
private function layerShowHandler(event:FlexEvent):void
{
// update the LODs/zoomslider to use/show the levels for the selected base map
var tiledLayer:TiledMapServiceLayer = event.target as TiledMapServiceLayer;
myMap.lods = tiledLayer.tileInfo.lods;
}
]]>
</fx:Script>
<s:Panel>
<esri:Map id="myMap" level="4" width="500" height="400" mapClick="onMapClick(event)">
<esri:ArcGISTiledMapServiceLayer
visible="{bb.selectedIndex == 0}"
show="layerShowHandler(event)"
url="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Portland/Portland_ESRI_LandBase_AGO/MapServer" />
<esri:ArcGISTiledMapServiceLayer
visible="{bb.selectedIndex == 1}"
show="layerShowHandler(event)"
url="http://server.arcgisonline.com/ArcGIS/rest/services/USA_Topo_Maps/MapServer" />
<esri:ArcGISTiledMapServiceLayer
visible="{bb.selectedIndex == 2}"
show="layerShowHandler(event)"
url="http://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer" />
</esri:Map>
<s:ButtonBar id="bb" selectedIndex="0" right="5" top="5">
<s:ArrayCollection>
<fx:Array>
<fx:String>Streets</fx:String>
<fx:String>U.S. Topo</fx:String>
<fx:String>Imagery</fx:String>
</fx:Array>
</s:ArrayCollection>
</s:ButtonBar>
</s:Panel>
<s:Panel x="501">
<s:VGroup width="100" height="400">
<s:Label id="lblLatitude" text="Latitude"/>
<s:TextInput id="latitudeText" text="" width="75"/>
<s:Label id="lblLongitude" text="Longitude"/>
<s:TextInput id="longitudeText" text="" width="75"/>
</s:VGroup>
</s:Panel>
</s:Application>
Thank you,Ruy