Select to view content in your preferred language

How to select points inside a selected Polygon graphic using the Draw Tool?

1032
5
Jump to solution
07-17-2012 11:06 AM
ionarawilson1
Deactivated User
I would like to use the freehand tool and multipoint tool to select companies. The freehand tool works great. When I create a polygon it creates a polygon graphic and shows point for the companies inside the polygon I created. For the multipoint I would like to use points to select counties that would show companies inside those counties. The company layer is in one service and the county layer is in another but I could have them in just one service if necessary. So far I have been able to use the tool to select the counties, but my question is how can I show the points (the company) for the counties selected?  I haven't found any similar example. Here is the code snippet I have so far:



  private function init():void    {     //things to do when the page creation is complete     drawToolbar.fillSymbol = fillSymbol;     drawToolbar.lineSymbol = lineSymbol;     drawToolbar.markerSymbol = drawPointSymbol;     drawToolbar.map = myMap;     drawToolbar.addEventListener(DrawEvent.DRAW_END,onDrawEnd, false, 0, true);                                }                   private function itemClickHandler(event:ItemClickEvent):void    {     //This function handles button clicks     freehandbutton.validateProperties();     myGraphicsLayer.clear();          //clear the graphics layer     myPolygonGraphicsLayer.clear();           //clear away all infoWindows     myMap.infoWindow.hide();          //clear the DataGrid    /*  var tempArray:Array = new Array(0);     tempArray = null; */    /*  querydg.dataProvider = tempArray; */          drawToolbar.fillSymbol = fillSymbol;          if (freehandbutton.selectedIndex < 0)     {      // when toggling a tool off, deactivate it      drawToolbar.deactivate();     }else{      switch (event.label)      {       case "Polygon Select":       {        drawToolbar.activate(DrawTool.POLYGON);        myMap.panEnabled = false;        break;       }       case "Freehand Polygon Select":       {        drawToolbar.activate(DrawTool.FREEHAND_POLYGON);        myMap.panEnabled = false;        break;       }       case "Multi Point Select":       {        drawToolbar.activate(DrawTool.MULTIPOINT);        myMap.panEnabled = false;        break;       }                          }     }    }         private function onDrawEnd(event:DrawEvent):void    {     drawToolbar.deactivate();      myGraphicsLayer.clear();     freehandbutton.selectedIndex = -1;     multipointbutton.selectedIndex = -1;     myMap.panEnabled = false;          if (event.graphic.geometry is Polygon)     {      var drawPolygon : Graphic = new Graphic();                   drawPolygon.geometry = event.graphic.geometry;      drawPolygon.autoMoveToTop = false; //keep graphics from moving to top               myPolygonGraphicsLayer.add(drawPolygon);       runQueryTask(event.graphic.geometry);          } else{      //Must be multipoint      runQueryTask3(event.graphic.geometry);}                          }           [Bindable] private var queryTask3:QueryTask = new QueryTask();    [Bindable] private var query3:Query = new Query();        private function runQueryTask3(geometry:Geometry):void    {               queryTask3.url = "http://tfs-24279/ArcGIS/rest/services/ForestProducts/county_forest_products/MapServer/0";     queryTask3.showBusyCursor = true;     queryTask.useAMF = false;               query3.geometry = geometry;      //geometry from the drawToolbar     query3.returnGeometry = true;    //set to true because we want to place points on the map     query3.spatialRelationship = "esriSpatialRelIntersects";     query3.outSpatialReference = myMap.spatialReference;     query3.outFields = ['*'];                             //run the query task                         queryTask3.execute(query3, new AsyncResponder( onResult, onFault));          function onResult(featureSet:FeatureSet, token:Object = null):void     {      if (featureSet.features.length == 0){       Alert.show("No matching records found. Please try again.");       resizableDraggableTitleWindow.visible = false;              querydg2.visible = false;                   }else{       myGraphicslayer.clear()                  myGraphicslayer.visible = true;                           for each(var graphic : Graphic in featureSet.features)       {                graphic.symbol = fillSymbol;        myGraphicslayer.add(graphic);                   }                                                  }            if (featureSet.features.length > 1) {       info.text = "There are " + featureSet.features.length + " matching records";      }      if (featureSet.features.length == 1) {       info.text = "There is " + featureSet.features.length + " matching record";      }             }          function onFault(info:Object, token:Object = null):void     {      Alert.show(info.toString(), "Query Problem");     }    }             
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus
Ionara,

   Something like this (untested code):

            private function init():void             {                 //things to do when the page creation is complete                 drawToolbar.fillSymbol = fillSymbol;                 drawToolbar.lineSymbol = lineSymbol;                 drawToolbar.markerSymbol = drawPointSymbol;                 drawToolbar.map = myMap;                 drawToolbar.addEventListener(DrawEvent.DRAW_END,onDrawEnd, false, 0, true);                 }                          private function itemClickHandler(event:ItemClickEvent):void             {                 //This function handles button clicks                 freehandbutton.validateProperties();                 myGraphicsLayer.clear();                                  //clear the graphics layer                 myPolygonGraphicsLayer.clear();                                   //clear away all infoWindows                 myMap.infoWindow.hide();                                  drawToolbar.fillSymbol = fillSymbol;                                  if (freehandbutton.selectedIndex < 0){                     // when toggling a tool off, deactivate it                     drawToolbar.deactivate();                 }else{                     switch (event.label){                         case "Polygon Select":{                             drawToolbar.activate(DrawTool.POLYGON);                             myMap.panEnabled = false;                             break;                         }                         case "Freehand Polygon Select":{                             drawToolbar.activate(DrawTool.FREEHAND_POLYGON);                             myMap.panEnabled = false;                             break;                         }                         case "Multi Point Select":{                             drawToolbar.activate(DrawTool.MULTIPOINT);                             myMap.panEnabled = false;                             break;                         }                                         }                 }             }                                       private function onDrawEnd(event:DrawEvent):void             {                 drawToolbar.deactivate();                  myGraphicsLayer.clear();                 freehandbutton.selectedIndex = -1;                 multipointbutton.selectedIndex = -1;                 myMap.panEnabled = false;                                  if (event.graphic.geometry is Polygon)                 {                     var drawPolygon : Graphic = new Graphic();                            drawPolygon.geometry = event.graphic.geometry;                     drawPolygon.autoMoveToTop = false; //keep graphics from moving to top                        myPolygonGraphicsLayer.add(drawPolygon);                      runQueryTask(event.graphic.geometry);                                      } else{                     //Must be multipoint                     runQueryTask3(event.graphic.geometry);                 }             }                                       [Bindable] private var queryTask3:QueryTask = new QueryTask();             [Bindable] private var query3:Query = new Query();                          [Bindable] private var queryTask4:QueryTask = new QueryTask();             [Bindable] private var query4:Query = new Query();                          private function runQueryTask3(geometry:Geometry):void             {                 queryTask3.url = "http://tfs-24279/ArcGIS/rest/services/ForestProducts/county_forest_products/MapServer/0";                 queryTask3.showBusyCursor = true;                 queryTask3.useAMF = false;                  query3.geometry = geometry;      //geometry from the drawToolbar                 query3.returnGeometry = true;    //set to true because we want to place points on the map                 query3.spatialRelationship = "esriSpatialRelIntersects";                 query3.outSpatialReference = myMap.spatialReference;                 query3.outFields = ['*'];                     //run the query task                 queryTask3.execute(query3, new AsyncResponder( onResult, onFault));                                  function onResult(featureSet:FeatureSet, token:Object = null):void                 {                     if (featureSet.features.length == 0){                         Alert.show("No matching records found. Please try again.");                         resizableDraggableTitleWindow.visible = false;                         querydg2.visible = false;                     }else{                         myGraphicslayer.clear()                         myGraphicslayer.visible = true;                         for each(var graphic : Graphic in featureSet.features){                             //graphic.symbol = fillSymbol;                             //myGraphicslayer.add(graphic);                                                          //Now run this query                             //Of course this url needs to be to the layer that has your companies                             queryTask4.url = "http://tfs-24279/ArcGIS/rest/services/ForestProducts/county_forest_products/MapServer/0";                             queryTask4.showBusyCursor = true;                             queryTask4.useAMF = false;                                                          query4.geometry = graphic.geometry;      //geometry from the first query                             query4.returnGeometry = true;    //set to true because we want to place points on the map                             query4.spatialRelationship = "esriSpatialRelIntersects";                             query4.outSpatialReference = myMap.spatialReference;                             query4.outFields = ['*'];                                 //run the query task                             queryTask4.execute(query4, new AsyncResponder( onResult2, onFault));                         }                         }                                          if (featureSet.features.length > 1) {                         info.text = "There are " + featureSet.features.length + " matching records";                     }                     if (featureSet.features.length == 1) {                         info.text = "There is " + featureSet.features.length + " matching record";                     }                 }                                  function onResult2(featureSet:FeatureSet, token:Object = null):void                 {                     if (featureSet.features.length == 0){                         Alert.show("No matching records found. Please try again.");                         resizableDraggableTitleWindow.visible = false;                         querydg2.visible = false;                     }else{                         myGraphicslayer.clear()                         myGraphicslayer.visible = true;                         for each(var graphic : Graphic in featureSet.features){                             graphic.symbol = fillSymbol;                             myGraphicslayer.add(graphic);                         }                     }                 }                                  function onFault(info:Object, token:Object = null):void                 {                     Alert.show(info.toString(), "Query Problem");                 }             }

View solution in original post

0 Kudos
5 Replies
RobertScheitlin__GISP
MVP Emeritus
Ionara,

   Something like this (untested code):

            private function init():void             {                 //things to do when the page creation is complete                 drawToolbar.fillSymbol = fillSymbol;                 drawToolbar.lineSymbol = lineSymbol;                 drawToolbar.markerSymbol = drawPointSymbol;                 drawToolbar.map = myMap;                 drawToolbar.addEventListener(DrawEvent.DRAW_END,onDrawEnd, false, 0, true);                 }                          private function itemClickHandler(event:ItemClickEvent):void             {                 //This function handles button clicks                 freehandbutton.validateProperties();                 myGraphicsLayer.clear();                                  //clear the graphics layer                 myPolygonGraphicsLayer.clear();                                   //clear away all infoWindows                 myMap.infoWindow.hide();                                  drawToolbar.fillSymbol = fillSymbol;                                  if (freehandbutton.selectedIndex < 0){                     // when toggling a tool off, deactivate it                     drawToolbar.deactivate();                 }else{                     switch (event.label){                         case "Polygon Select":{                             drawToolbar.activate(DrawTool.POLYGON);                             myMap.panEnabled = false;                             break;                         }                         case "Freehand Polygon Select":{                             drawToolbar.activate(DrawTool.FREEHAND_POLYGON);                             myMap.panEnabled = false;                             break;                         }                         case "Multi Point Select":{                             drawToolbar.activate(DrawTool.MULTIPOINT);                             myMap.panEnabled = false;                             break;                         }                                         }                 }             }                                       private function onDrawEnd(event:DrawEvent):void             {                 drawToolbar.deactivate();                  myGraphicsLayer.clear();                 freehandbutton.selectedIndex = -1;                 multipointbutton.selectedIndex = -1;                 myMap.panEnabled = false;                                  if (event.graphic.geometry is Polygon)                 {                     var drawPolygon : Graphic = new Graphic();                            drawPolygon.geometry = event.graphic.geometry;                     drawPolygon.autoMoveToTop = false; //keep graphics from moving to top                        myPolygonGraphicsLayer.add(drawPolygon);                      runQueryTask(event.graphic.geometry);                                      } else{                     //Must be multipoint                     runQueryTask3(event.graphic.geometry);                 }             }                                       [Bindable] private var queryTask3:QueryTask = new QueryTask();             [Bindable] private var query3:Query = new Query();                          [Bindable] private var queryTask4:QueryTask = new QueryTask();             [Bindable] private var query4:Query = new Query();                          private function runQueryTask3(geometry:Geometry):void             {                 queryTask3.url = "http://tfs-24279/ArcGIS/rest/services/ForestProducts/county_forest_products/MapServer/0";                 queryTask3.showBusyCursor = true;                 queryTask3.useAMF = false;                  query3.geometry = geometry;      //geometry from the drawToolbar                 query3.returnGeometry = true;    //set to true because we want to place points on the map                 query3.spatialRelationship = "esriSpatialRelIntersects";                 query3.outSpatialReference = myMap.spatialReference;                 query3.outFields = ['*'];                     //run the query task                 queryTask3.execute(query3, new AsyncResponder( onResult, onFault));                                  function onResult(featureSet:FeatureSet, token:Object = null):void                 {                     if (featureSet.features.length == 0){                         Alert.show("No matching records found. Please try again.");                         resizableDraggableTitleWindow.visible = false;                         querydg2.visible = false;                     }else{                         myGraphicslayer.clear()                         myGraphicslayer.visible = true;                         for each(var graphic : Graphic in featureSet.features){                             //graphic.symbol = fillSymbol;                             //myGraphicslayer.add(graphic);                                                          //Now run this query                             //Of course this url needs to be to the layer that has your companies                             queryTask4.url = "http://tfs-24279/ArcGIS/rest/services/ForestProducts/county_forest_products/MapServer/0";                             queryTask4.showBusyCursor = true;                             queryTask4.useAMF = false;                                                          query4.geometry = graphic.geometry;      //geometry from the first query                             query4.returnGeometry = true;    //set to true because we want to place points on the map                             query4.spatialRelationship = "esriSpatialRelIntersects";                             query4.outSpatialReference = myMap.spatialReference;                             query4.outFields = ['*'];                                 //run the query task                             queryTask4.execute(query4, new AsyncResponder( onResult2, onFault));                         }                         }                                          if (featureSet.features.length > 1) {                         info.text = "There are " + featureSet.features.length + " matching records";                     }                     if (featureSet.features.length == 1) {                         info.text = "There is " + featureSet.features.length + " matching record";                     }                 }                                  function onResult2(featureSet:FeatureSet, token:Object = null):void                 {                     if (featureSet.features.length == 0){                         Alert.show("No matching records found. Please try again.");                         resizableDraggableTitleWindow.visible = false;                         querydg2.visible = false;                     }else{                         myGraphicslayer.clear()                         myGraphicslayer.visible = true;                         for each(var graphic : Graphic in featureSet.features){                             graphic.symbol = fillSymbol;                             myGraphicslayer.add(graphic);                         }                     }                 }                                  function onFault(info:Object, token:Object = null):void                 {                     Alert.show(info.toString(), "Query Problem");                 }             }
0 Kudos
ionarawilson1
Deactivated User
Thank you so much for your help Robert. The only things I added were symbols for the county and the company and I uncommented the graphicsLayer.clear() method on the onResult2 function. However, there is an issue: I can selected the record with the point tool, but it is only selecting the first county I click on. For example, let's say I click in two counties, if the first one has companies, those counties and the points inside get selected, but the second county is not selected and the companies inside that second county don't appear either. If I select two records, the first one without companies and the second one with companies, the first county gets selected but I get a message "no matching records found", and the second one (that has companies ) does not get selected and neither are the companies in that second county. Any ideas why this is happening? Thank you! Here is the code:



   [Bindable] private var queryTask3:QueryTask = new QueryTask();
   [Bindable] private var query3:Query = new Query();
  
   [Bindable] private var queryTask4:QueryTask = new QueryTask();
   [Bindable] private var query4:Query = new Query();
   

   
   
   
   private function runQueryTask3(geometry:Geometry):void
   {
    
    
    queryTask3.url = "http://tfs-24279/ArcGIS/rest/services/ForestProducts/county_forest_products/MapServer/0";
    queryTask3.showBusyCursor = true;
    queryTask3.useAMF = false;
    
    
    query3.geometry = geometry;      //geometry from the drawToolbar
    query3.returnGeometry = true;    //set to true because we want to place points on the map
    query3.spatialRelationship = "esriSpatialRelIntersects";
    query3.outSpatialReference = myMap.spatialReference;
    query3.outFields = ['*'];    
    
    
    
    
    //run the query task
    
    
    
    queryTask3.execute(query3, new AsyncResponder( onResult, onFault));
    
    function onResult(featureSet:FeatureSet, token:Object = null):void
    {
     if (featureSet.features.length == 0){
      Alert.show("No matching records found. Please try again.");
      resizableDraggableTitleWindow.visible = false;
      
      querydg2.visible = false;
     
      
     }
     
     else{
      myGraphicslayer.clear()
      myGraphicslayer.visible = true;
     
      
      
      for each(var graphic : Graphic in featureSet.features)
      graphic.symbol = fillSymbolmultipoint;      myGraphicslayer.add(graphic);    
       
     
      
      //Now run this query
      //Of course this url needs to be to the layer that has your companies
      {
       
       queryTask4.url = "http://tfs-24279/ArcGIS/rest/services/ForestProducts/dynamic_layer_forest_products/MapServer/0";
      
       queryTask4.showBusyCursor = true;
       queryTask4.useAMF = false;
      
       query4.geometry = graphic.geometry;      //geometry from the first query
       query4.returnGeometry = true;    //set to true because we want to place points on the map
       query4.spatialRelationship = "esriSpatialRelIntersects";
       query4.outSpatialReference = myMap.spatialReference;
       query4.outFields = ['*'];    
       //run the query task
       queryTask4.execute(query4, new AsyncResponder(onResult2, onFault));
       
      
      }
      
      
      
     }
     
     if (featureSet.features.length > 1) {
      info.text = "There are " + featureSet.features.length + " matching records";
     }
     if (featureSet.features.length == 1) {
      info.text = "There is " + featureSet.features.length + " matching record";
     }  
     
    }
    
    function onFault(info:Object, token:Object = null):void
    {
     Alert.show(info.toString(), "Query Problem");
    }
   }
      
   
   public function onResult2(featureSet:FeatureSet, token:Object = null):void
   {
    if (featureSet.features.length == 0){
     Alert.show("No matching records found. Please try again.");
     resizableDraggableTitleWindow.visible = false;
     querydg2.visible = false;
    }else{
     //myGraphicslayer.clear()
     myGraphicslayer.visible = true;
     for each(var graphic : Graphic in featureSet.features){
      graphic.symbol = resultsSymbol;      myGraphicslayer.add(graphic);
     }
    }
   }

   





0 Kudos
ionarawilson1
Deactivated User
Sorry, I forgot to include the whole code, it seems that it actually selects the last county I click. However, the text on the panel says that "There are 2 matching records", but just the second one that I click appears selected and the companies in it appear, but not the first county.

Here is the complete code snippet


 [Bindable] private var queryTask3:QueryTask = new QueryTask();
   [Bindable] private var query3:Query = new Query();
  
   [Bindable] private var queryTask4:QueryTask = new QueryTask();
   [Bindable] private var query4:Query = new Query();
   

   
   private function runQueryTask3(geometry:Geometry):void
   {
    queryTask3.url = "http://tfs-24279/ArcGIS/rest/services/ForestProducts/county_forest_products/MapServer/0";
    queryTask3.showBusyCursor = true;
    queryTask3.useAMF = false;
    
    query3.geometry = geometry;      //geometry from the drawToolbar
    query3.returnGeometry = true;    //set to true because we want to place points on the map
    query3.spatialRelationship = "esriSpatialRelIntersects";
    query3.outSpatialReference = myMap.spatialReference;
    query3.outFields = ['*'];    
    //run the query task
    queryTask3.execute(query3, new AsyncResponder( onResult, onFault));
    
    function onResult(featureSet:FeatureSet, token:Object = null):void
    {
     if (featureSet.features.length == 0){
      Alert.show("No matching records found. Please try again.");
      resizableDraggableTitleWindow.visible = false;
      querydg2.visible = false;
     }else{
      myGraphicslayer.clear()
      myGraphicslayer.visible = true;
      for each(var graphic : Graphic in featureSet.features){
       graphic.symbol = fillSymbolmultipoint;        //myGraphicslayer.add(graphic);
       
       //Now run this query
       //Of course this url needs to be to the layer that has your companies
       queryTask4.url = "http://tfs-24279/ArcGIS/rest/services/ForestProducts/county_forest_products/MapServer/0";
       queryTask4.showBusyCursor = true;
       queryTask4.useAMF = false;
       
       query4.geometry = graphic.geometry;      //geometry from the first query
       query4.returnGeometry = true;    //set to true because we want to place points on the map
       query4.spatialRelationship = "esriSpatialRelIntersects";
       query4.outSpatialReference = myMap.spatialReference;
       query4.outFields = ['*'];    
       //run the query task
       queryTask4.execute(query4, new AsyncResponder( onResult2, onFault));
      }    
     }
     
     if (featureSet.features.length > 1) {
      info.text = "There are " + featureSet.features.length + " matching records";
     }
     if (featureSet.features.length == 1) {
      info.text = "There is " + featureSet.features.length + " matching record";
     }
    }
    
    function onResult2(featureSet:FeatureSet, token:Object = null):void
    {
     if (featureSet.features.length == 0){
      Alert.show("No matching records found. Please try again.");
      resizableDraggableTitleWindow.visible = false;
      querydg2.visible = false;
     }else{
      //myGraphicslayer.clear()      myGraphicslayer.visible = true;
      for each(var graphic : Graphic in featureSet.features){
       graphic.symbol = resultsSymbol;        myGraphicslayer.add(graphic);
      }
     }
    }
    
    function onFault(info:Object, token:Object = null):void
    {
     Alert.show(info.toString(), "Query Problem");
    }
   }
   

0 Kudos
ionarawilson1
Deactivated User
Ok, It was something wrong I was doing. When I select two counties with companies, they are selected now and the companies appear. The only issue now is when I select two counties for example and either one does not have a company. Then I get a message saing no records were found although one had a company and it was selected. Thanks!
0 Kudos
ionarawilson1
Deactivated User
I was able to get rid of the warning messages but I am still not sure about how to create a message when no companies exist in all the counties selected. I will end this thread and start a new one! Thank you so much Robert!
0 Kudos