Problem with adapting Hover sample: QueryTask does not execute

2219
5
Jump to solution
03-08-2013 11:42 AM
CharlesGeiger
New Contributor III
I am trying to adapt the JavaScript sample "Load query results and show on hover" to a map of my university campus which will let users hover over a building, highlight the building and produce a tooltip-type InfoWindow showing the building name (about 75 buildings on campus).  I have a tiled base map of the campus for the background, and created an additional map service (not tiled) of the buildings alone. It seems to me that the graphics layer approach of the sample should be fine; we don't have ArcSDE (yet) for the feature layer approach.  Everything performs just fine except for the QueryTask, about halfway down in the code snippet below.  You'll notice a couple of javascript alerts, which never fire.  I tried tracing it in Firebug, but get lost in the dojo part of the trail.  Any help will be appreciated.

      var queryTaskBldg = new esri.tasks.QueryTask("http://mapmaker.millersville.edu/ArcGIS/rest/services/MU_Buildings/MapServer/0");          //build query filter       var queryBldg = new esri.tasks.Query();         queryBldg.returnGeometry = true;         queryBldg.outFields = ["NAME"];         queryBldg.outSpatialReference = spaRef;  //  {"wkid":2272}         queryBldg.where = "USE = 'Academic'";  //  I don't want this, but I am trying to imitate the sample        sHover = "${NAME}";       hoverTemplate = new esri.InfoTemplate();         hoverTemplate.setTitle(sHover);         hoverTemplate.setContent("Name : ${NAME}");        map.infoWindow.resize(200,100);        dojo.connect(queryTaskBldg, "onComplete", function(featureSet) {  //THIS IS WHERE IT FAILS         alert("Hi");         map.graphics.clear();         var features = featureSet.features;         var graphic, attributes, bldgName;           //QueryTask returns a featureSet.  Loop through features in the featureSet and add them to the map.         for (var i=0, il=features.length; i<il; i++) {           //Get the current feature from the featureSet.           //Feature is a graphic           graphic = features;           attrib = graphic.attributes;           bldgName = attrib.NAME;           alert(bldgName);           graphic.setSymbol(esriFillSym);           graphic.setInfoTemplate(hoverTemplate);            //Add graphic to the counties graphics layer. //          map.graphics.add(graphic);            graphicsLayer.add(graphic);         }         map.addLayer(graphicsLayer);
0 Kudos
1 Solution

Accepted Solutions
BrettGreenfield__DNR_
Occasional Contributor II
It looks like the query task is never being told to run.  Check out this link on the API Reference page.  You'll have to have a line in your code somewehre that says queryTaskBldg.execute(parameters); you probably want to stick it in a function where you listen for an onMouseOver event over the graphic layer.

View solution in original post

0 Kudos
5 Replies
BrettGreenfield__DNR_
Occasional Contributor II
It looks like the query task is never being told to run.  Check out this link on the API Reference page.  You'll have to have a line in your code somewehre that says queryTaskBldg.execute(parameters); you probably want to stick it in a function where you listen for an onMouseOver event over the graphic layer.
0 Kudos
StephenLead
Regular Contributor III
It seems to me that the graphics layer approach of the sample should be fine; we don't have ArcSDE (yet) for the feature layer approach


You don't need ArcSDE to use a featureLayer - this is supported when using a file geodatabase. See the feature layer hover sample for an example of hovering over a feature to obtain information, without needing to run a query.

The advantage to this approach is that you vastly reduce the amount of traffic/server requests, and you'll also see better performance as the attributes are already present on the client.

Steve
0 Kudos
CharlesGeiger
New Contributor III
Thanks for your suggestions.  I did get it to work, but am not really sure what did the trick.  As bgfield suggested, I did add an explicit call by first wrapping the lines from the first alert to the end of the code sample in a function declaration and then replacing the
dojo.connect(queryTaskBldg, "onComplete", function(featureSet) ...
with
queryTaskBldg.execute(parameters);
.  At the same time, I was experimenting with different JavaScript API and CSS version values.  The mouse-based events worked as in the sample, once the function call worked.

My question, if anyone can help me to understand some areas of JavaScript and/or the ArcGIS JavaScript API that I am still trying to master, is whether the
dojo.connect(queryTaskBldg, "onComplete", function(featureSet) ...
statement is the equivalent to a function call.  What fires the dojo.connect (I assumed that "onComplete" meant that it executes when the map has drawn its initial contents)?  And what does dojo.connect connect?
0 Kudos
StephenLead
Regular Contributor III
What fires the dojo.connect (I assumed that "onComplete" meant that it executes when the map has drawn its initial contents)?  And what does dojo.connect connect?


dojo.connect is a listener meaning that it will sit there idle, until the event for which it is waiting occurs. In this case, he event for which it's waiting is the on-complete event of the queryTask (queryTaskBldg) - see the Events section of QueryTask.

So, the dojo.connect "fires" once the query task has completed.
0 Kudos
CharlesGeiger
New Contributor III
Steve, thank you for your suggestion.  I had been under the impression that a featureLayer required a feature service (and therefore ArcSDE).  Now I see the difference.  I like the tooltip customization in that sample, so I may switch to that approach.  Thanks.

Just saw your second reply.  Thank you for that clarification.  That really helps.
0 Kudos