Select to view content in your preferred language

Test Query If User Clicks Within a Polygon

2887
2
06-16-2010 04:06 PM
RuyMartinez
Emerging Contributor
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
Tags (2)
0 Kudos
2 Replies
RobertScheitlin__GISP
MVP Emeritus
Ruy,

   Here is a functioning version of your test app. There where a couple of major things.

1. You where trying to mix cached base maps with different WKIDs (cant do that with cached map services)

2. You where never calling execute on the query task.

<?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 = event.mapPoint;
    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 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 = mapPoint;
    myQuery.spatialRelationship = "esriSpatialRelWithin";
    myQuery.returnGeometry = true;
  
    queryTask.execute(myQuery,new AsyncResponder(onResult,onFault));
    
    // Did the user click within a polygon
    function onResult(featureSet:FeatureSet, token:Object = null):void
    {
     if(featureSet.features){
      if(featureSet.features.length > 0){
       Alert.show("You clicked inside the polygon");
      } else {
       Alert.show("You clicked outside the polygon");
      }
     }
    }
    
    function onFault(info:Object, token:Object = null):void
    {
     Alert.show("Error Occured");
    }
   }
   
   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/NGS_Topo_US_2D/MapServer" />
   <esri:ArcGISTiledMapServiceLayer
    visible="{bb.selectedIndex == 2}"
    show="layerShowHandler(event)"
    url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_Imagery_World_2D/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>
0 Kudos
RuyMartinez
Emerging Contributor
Robert,

This is what I was trying to do.  I will go back and read up on cached services and try to remember that an event cannot listen until I tell it to.:D

Thank you for looking at this and cleaning up the code.

Ruy
0 Kudos