Select to view content in your preferred language

QueryTask output behind?

645
2
02-16-2011 07:46 PM
JayGregory
Occasional Contributor
Hopefully this will be the last issue I have:
In short, I've created a Query that detects whether a mouse click is within a polygon.  If it is, a point is placed on the map, if not, an error is displayed.  It works most of the time except the first time the mouse click is in a different location (with regards to in polygon/ outside polygon) than the previous. 
For instance, when the map is loaded, if I click inside the polygon, nothing happens.  Then if I click inside the polygon again, behavior is as expected.  I can continue clicking inside the polygon and behavior is as expected.  As soon as I click outside the polygon for the first time, it acts as if I've clicked inside the polygon (creating the graphic, etc.).  Then if I click outside the polygon a second time, behavior is as expected (no graphic, warning, etc.).  I can continue clicking outside the polygon again and again, and it works.  Until I click inside the polygon again, at which point it still thinks I'm clicking outside, but only for the first click.  And so on and so forth. 
Any ideas?  Do I have to specify some attribute in the QueryTaks, or is there something else I am missing?  Thanks!

Here is the function:
private function onMapClick(event:MapMouseEvent):void
   {
    query.geometry = event.mapPoint;
    queryTask.execute(query);
    var num:int = queryTask.executeLastResult.features.length;
    Alert.show(num.toString());
    if ( num != 1){
     Alert.show("All stops must be located inside Philadelphia County");
     return;
    }
    var point:MapPoint = event.mapPoint;
    var myGraphicMarker:Graphic = new Graphic(point,userMarks);
    myGraphicMarker.toolTip = "Added stop";
    userClicks.add(myGraphicMarker);
    stops.push(myGraphicMarker);
    messages.text = stops.length.toString() + " total stops"
   
   }
Tags (2)
0 Kudos
2 Replies
MehulChoksey
Esri Contributor
When you make a query request to the server, you have to wait for the results to come(unless you are binding the executelastresult to a component). This can be achieved by either:
1.Using Responder sample for using the same is at: http://help.arcgis.com/en/webapi/flex/samples/index.html?sample=QueryResultsWithChart
2.  Adding executeComplete event listener to querytask and in event handler perform rest of your steps:
i.e.
var num:int = queryTask.executeLastResult.features.length;
    Alert.show(num.toString());
    if ( num != 1){
     Alert.show("All stops must be located inside Philadelphia County");
     return;
    }
    var point:MapPoint = event.mapPoint;
    var myGraphicMarker:Graphic = new Graphic(point,userMarks);
    myGraphicMarker.toolTip = "Added stop";
    userClicks.add(myGraphicMarker);
    stops.push(myGraphicMarker);
    messages.text = stops.length.toString() + " total stops"
0 Kudos
JayGregory
Occasional Contributor
Thank you so much - this was the answer. 

When you make a query request to the server, you have to wait for the results to come(unless you are binding the executelastresult to a component). This can be achieved by either:
1.Using Responder sample for using the same is at: http://help.arcgis.com/en/webapi/flex/samples/index.html?sample=QueryResultsWithChart
2.  Adding executeComplete event listener to querytask and in event handler perform rest of your steps:
i.e.
var num:int = queryTask.executeLastResult.features.length;
    Alert.show(num.toString());
    if ( num != 1){
     Alert.show("All stops must be located inside Philadelphia County");
     return;
    }
    var point:MapPoint = event.mapPoint;
    var myGraphicMarker:Graphic = new Graphic(point,userMarks);
    myGraphicMarker.toolTip = "Added stop";
    userClicks.add(myGraphicMarker);
    stops.push(myGraphicMarker);
    messages.text = stops.length.toString() + " total stops"
0 Kudos