Select to view content in your preferred language

Updating Find Task Woes

1382
18
Jump to solution
11-04-2013 07:03 AM
AlexDeVine
New Contributor III
Morning,

In my quest to update one of my web apps to AMD style, I am running into a perplexing issue with my find task. The structure of this task is found in many places in the Samples. The following code is my version of it on my app:

Global variables, require and function calls
 var map, navToolbar, infoTemplate, identifyTask, identifyParams, findTask, findParams, graphicsLayer, searchText;         require(["dojo/parser", "dojo/_base/connect", "dojo/_base/array", "esri/map", "esri/layers/ArcGISDynamicMapServiceLayer", "esri/layers/GraphicsLayer", "esri/graphic", "esri/InfoTemplate", "esri/toolbars/navigation", "dojo/dom", "esri/tasks/FindTask",             "esri/tasks/FindParameters", "dijit/dijit", "dijit/layout/BorderContainer", "dijit/layout/ContentPane",             "dijit/layout/AccordionContainer", "dijit/form/TextBox", "dijit/form/Button", "dijit/Toolbar", "dijit/Tooltip", "dijit/registry",             "dojo/_base/Color", "esri/symbols/SimpleMarkerSymbol", "esri/symbols/SimpleLineSymbol", "esri/symbols/SimpleFillSymbol",             "esri/tasks/IdentifyTask", "esri/tasks/IdentifyParameters", "esri/tasks/IdentifyResult", "dojo/domReady!"         ], function (           parser, connect, array, Map, ArcGISDynamicMapServiceLayer, GraphicsLayer, Graphic, InfoTemplate, Navigation, dom, FindTask,           FindParameters, Color, SimpleMarkerSymbol, SimpleLineSymbol, SimpleFillSymbol, IdentifyTask, IdentifyParameters, IdentifyResult, registry         )


The find task:

function execute(searchText) {             //set the search text to find parameters             //create find tasks with url to map services             findTask = new esri.tasks.FindTask("http://eagle.camplan.uga.edu:6080/arcgis/rest/services/CampusMap/UGA_CampusMap_Base/MapServer");             //create find parameters and define known values             findParams = new esri.tasks.FindParameters();                          findParams.returnGeometry = true;             //findParams.outSpatialReference = map.spatialReference;             findParams.layerIds = [7];             findParams.searchFields = ["NAME", "NUMBER"];                          findParams.searchText = searchText;              findTask.execute(findParams, showResults, findErr);          }          function showResults(results) {              //display the results of the building text search             //symbology for graphics             markerSymbol = new esri.symbol.SimpleMarkerSymbol(esri.symbol.SimpleMarkerSymbol.STYLE_SQUARE, 20, new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([0, 255, 255]), 1), new dojo.Color([0, 255, 255, 0.25]));             lineSymbol = new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_DASH, new dojo.Color([0, 255, 255]), 1);             polygonSymbol = new esri.symbol.SimpleFillSymbol(esri.symbol.SimpleFillSymbol.STYLE_NONE, new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_DASHDOT, new dojo.Color([0, 255, 255]), 2), new dojo.Color([0, 255, 255, 0.25]));              //find results return an array of graphics in the graphicslayer.             graphicsLayer = new esri.layers.GraphicsLayer();             graphicsLayer.spatialReference = map.spatialReference;             map.graphics.clear();              //Build an array of attribute information and add each found graphic to the map             dojo.forEach(results, function (result) {                 var graphic = new esri.Graphic(result.feature);                 graphic.spatialReference = map.spatialReference;                 graphic.setSymbol(polygonSymbol);                 map.graphics.add(graphic);                 graphicsLayer.add(graphic);             });              //create an extent matching the graphics of the parcel(s)             var zoomExtent = esri.graphicsExtent(graphicsLayer.graphics);             var zoomXmin = zoomExtent.xmin;             var zoomXmax = zoomExtent.xmax;             var zoomScale = zoomXmax - zoomXmin;             if (zoomExtent.xmin < 2460000 || zoomExtent.ymin < 1345000 || zoomExtent.xmax > 2630000 || zoomExtent.ymax > 1505000) {                 apprise("Your Find Building results include a building outside of ACC/Main UGA Campus. Please refine your search");             }             else if (zoomScale > 10000) {                 apprise("Your Find Building results require a coarse scale to display. Please refine your search");             }             else {                 map.setExtent(zoomExtent.expand(2));             }         }          function findErr(error) {             //Alerts the user to a return of no results in building search             apprise("Your search returned no results. Please refine your search.");         }


The find task function showResults is failing to findErr at the point when I attempt to add the graphic to the map and to the graphicsLayer:

                map.graphics.add(graphic);                 graphicsLayer.add(graphic);


If I comment out these add statements, I fail out at the extent creation, but one thing at a time, right? 🙂

Anyone have any ideas on why I cannot add the graphics to the map or to the graphics layer? I tried to address any issue that spatial reference might cause with setting the graphics returned from the FindTask and the GraphicsLayer to the map Spatial Reference, but I am at a loss.

Alex DeVine
0 Kudos
18 Replies
KenBuja
MVP Esteemed Contributor
I agree that everything looks OK in your code. Now here's a shot in the dark. Where do your functions that use FindTask and IdentifyTask reside? Are the within that require function like this?


require(["dojo/ready", etc]
    function (ready, etc) {

        function execute(searchText) {
            //set the search text to find parameters
            //create find tasks with url to map services
            findTask = new esri.tasks.FindTask("http://eagle.camplan.uga.edu:6080/arcgis/rest/services/CampusMap/UGA_CampusMap_Base/MapServer");
            //create find parameters and define known values
            findParams = new esri.tasks.FindParameters();
            
            findParams.returnGeometry = true;
            //findParams.outSpatialReference = map.spatialReference;
            findParams.layerIds = [7];
            findParams.searchFields = ["NAME", "NUMBER"];
            
            findParams.searchText = searchText;

            findTask.execute(findParams, showResults, findErr);

        }
    });


or do you have it outside that function, like this?

require(["dojo/ready", etc]
    function (ready, etc) {

    });

    function execute(searchText) {
        //set the search text to find parameters
        //create find tasks with url to map services
        findTask = new  esri.tasks.FindTask("http://eagle.camplan.uga.edu:6080/arcgis/rest/services/CampusMap/UGA_CampusMap_Base/MapServer");
        //create find parameters and define known values
        findParams = new esri.tasks.FindParameters();
            
        findParams.returnGeometry = true;
        //findParams.outSpatialReference = map.spatialReference;
        findParams.layerIds = [7];
        findParams.searchFields = ["NAME", "NUMBER"];
            
        findParams.searchText = searchText;

        findTask.execute(findParams, showResults, findErr);

    }


If it's like the second version, then they won't operate without the dot notation.
0 Kudos
AlexDeVine
New Contributor III
Ken,

It is like the second one.  That is very helpful as to why some of my constructors want dot notation, but does it affect the behavior of the methods? I think my function is breaking on map.graphics.add(graphic) in the forEach loop applied for each result of my FindTask. I determined this using a series of console.logs and alerts. From those console logs, I also determined that my graphic did, in fact, exist but seemingly could not be added to the map using the aforementioned method.

Here is my whole showResults function, including my console.logs and alerts:

function showResults(results) {
            //display the results of the building text search

            //find results return an array of graphics in the graphicslayer.
            graphicsLayer = new esri.layers.GraphicsLayer();
            graphicsLayer.spatialReference = map.spatialReference;
            map.graphics.clear();

            //Build an array of attribute information and add each found graphic to the map
            dojo.forEach(results, function (result) {
                console.log(result.feature);
                console.log(result);
                alert("forEach Fired");
                graphic = new esri.Graphic(result.feature);
                graphic.spatialReference = map.spatialReference;
                console.log(graphic);
                graphic.setSymbol(polygonSymbol);
                alert("SymbolSet");
                console.log(graphicsLayer);
                console.log(map.spatialReference);
                console.log(graphic.spatialReference);
                alert("About to attempt to add graphics");
*****This is where it breaks!! I get the above alert, but not the next alert below***************
                map.graphics.add(graphic);
                alert("graphic added to map");
                graphicsLayer.add(graphic);
                alert("graphic added to graphicsLayer");
                alert("forEach looped");
            });

            //create an extent matching the graphics of the parcel(s)
            var zoomExtent = esri.graphicsExtent(graphicsLayer.graphics);
            var zoomXmin = zoomExtent.xmin;
            var zoomXmax = zoomExtent.xmax;
            var zoomScale = zoomXmax - zoomXmin;
            if (zoomExtent.xmin < 2460000 || zoomExtent.ymin < 1345000 || zoomExtent.xmax > 2630000 || zoomExtent.ymax > 1505000) {
                apprise("Your Find Building results include a building outside of ACC/Main UGA Campus. Please refine your search");
            }
            else if (zoomScale > 10000) {
                apprise("Your Find Building results require a coarse scale to display. Please refine your search");
            }
            else {
                map.setExtent(zoomExtent.expand(2));
            }
        }

        function findErr(error) {
            //Alerts the user to a return of no results in building search
            apprise("Your search returned no results. Please refine your search.");
        }


I am just not seeing any logic to why it would behave this way...
0 Kudos
TracySchloss
Frequent Contributor
Maybe this is too obvious, but do you have variables for graphic and graphicsLayer defined earlier?  I'm forever leaving out that little word 'var' when I get in a hurry.
0 Kudos
AlexDeVine
New Contributor III
Maybe this is too obvious, but do you have variables for graphic and graphicsLayer defined earlier?  I'm forever leaving out that little word 'var' when I get in a hurry.


I have been guilty of that, too, Tracy, but in this case, I seem to be on top of it. I have both of those declared as global variables.

Here is the url to my page, you can see for yourlsef the behavior I am getting for that Find Task.

http://eagle.camplan.uga.edu/development/CampusMap/CampusMap.html

Alex
0 Kudos
TracySchloss
Frequent Contributor
I can get the map to open, but your services must not be public facing because nothing loads after the frame.
0 Kudos
AlexDeVine
New Contributor III
I can get the map to open, but your services must not be public facing because nothing loads after the frame.


Sorry, Kelly... I have logged a request with our enterprise IT service to open the appropriate ports in our campus firewall. That was something I had overlooked in this conversion process.

Alex
0 Kudos
AlexDeVine
New Contributor III
Sorry, Kelly... I have logged a request with our enterprise IT service to open the appropriate ports in our campus firewall. That was something I had overlooked in this conversion process.

Alex


I meant Tracy... Sorry.

I believe the firewall is configured appropriately now...

http://eagle.camplan.uga.edu/development/CampusMap/CampusMap.html

Alex
0 Kudos
AlexDeVine
New Contributor III
Figured it out.

you used to could get away with this when constructing a graphic:

graphic = new esri.Graphic(result.feature);


it appears now that you have to be more descriptive:

graphic = new esri.Graphic(result.feature.geometry);


Lesson learned.

Thank you all for allowing me to bounce ideas and all the great help.

Alex
0 Kudos
TracySchloss
Frequent Contributor
I'm glad you got it figured out.  I have gotten turned around before with what class 'result' is that's getting returned.  Depending on what the task was you just executed, it isn't always the same.  Once I realized that, I started putting breakpoints in at the very top of my callback handler so I check right away, instead of focusing several lines down.
0 Kudos