Beginner: findTask does not seem to work

1207
5
09-23-2011 09:49 AM
CharlesGeiger
Regular Contributor
I have combined parts of two sample scripts: query and find.  The query (click a point to highlight the polygon that was clicked in and open an attribute pop-up) works fine.  The find (type a place name in a text box and click an input button to re-symbolize the found point) does not.  I would greatly appreciate another pair of eyes on this.  The map service layers are correct and the fieldnames are correct.  Place names (such as Coatesville or Lancaster) in the attribute table are in title case.  The relevant part of my HTML/JavaScript code is pasted here:

    <script type="text/javascript">
      dojo.require("dijit.layout.BorderContainer");
      dojo.require("dijit.layout.ContentPane");
      dojo.require("esri.map");
      dojo.require("esri.tasks.query");
      dojo.require("esri.tasks.find");

      var map;
      var featureSet, queryTask, query;
      var findPlace, findParams;

      function init() {
          //Map's initial extent:
        var initExtent = new esri.geometry.Extent({"xmin":-8980000,"ymin":4750000,"xmax":-8300000,"ymax":5260000,"spatialReference":{"wkid":102100}});
          //Create the map:
        map = new esri.Map("mapDiv",{extent:initExtent});

          //Creates the shaded relief basemap
        var basemapURL = "http://server.arcgisonline.com/ArcGIS/rest/services/World_Terrain_Base/MapServer";
        var basemap = new esri.layers.ArcGISTiledMapServiceLayer(basemapURL);
        map.addLayer(basemap);

          //Add the landforms layers
        var paMapURL= "https://mapmaker.millersville.edu/ArcGIS/rest/services/PA_Landforms/MapServer";
        var paMap = new esri.layers.ArcGISDynamicMapServiceLayer(paMapURL, {"opacity": 0.4});
        map.addLayer(paMap);

          //Add the places layers
        var paPlacesURL= "https://mapmaker.millersville.edu/ArcGIS/rest/services/PAplaces/MapServer";
        var paPlaces = new esri.layers.ArcGISDynamicMapServiceLayer(paPlacesURL, {"opacity": 0.8});
        map.addLayer(paPlaces);
       
          //Create the find task
        findPlace = new esri.tasks.FindTask(paPlacesURL+"/");
       
          //Create the find paramaters
        findParams = new esri.tasks.FindParameters();
        findParams.returnGeometry = true;
        findParams.layerIDs = [0];
        findParams.searchFields = ["AREANAME"];

          //Listen for Click event:       
        dojo.connect(map, "onClick", doQueryTask);
          //Listen for infoWindow Hide event:
        dojo.connect(map.infoWindow, "onHide", function() {map.graphics.clear();});

          //Build query task:
        queryTask = new esri.tasks.QueryTask (paMapURL+"/2");
       
          //Build query filter:
        query = new esri.tasks.Query();
          query.outSpatialReference = {"wkid":102100};
          query.returnGeometry = true;
          query.outFields = ["PROVINCE", "SECTION"];
      }
     
      function doQueryTask(evt)  {
        map.infoWindow.hide();
        map.graphics.clear();
        featureSet = null;
       
        //onClick event returns the point where the user clicked on the map as "evt":
        query.geometry = evt.mapPoint;
       
        //Executing the task:
        queryTask.execute(query, function(fset) {
          if (fset.features.length === 1) {
            showFeature(fset.features[0],evt);
          }
        });
      }
     
      function showFeature(feature,evt)  {
        map.graphics.clear();

          //Set the symbol:
        //var symbol = new esri.symbol.SimpleFillSymbol(esri.symbol.SimpleFillSymbol.STYLE_SOLID, new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([255,0,0]), 2), new dojo.Color([255,255,0,0.5]));
        //Set symbology
        var areaStyle = esri.symbol.SimpleFillSymbol.STYLE_SOLID;
        var areaOutlineStyle = esri.symbol.SimpleLineSymbol.STYLE_SOLID;
        var areaOutlineColor = new dojo.Color([244,0,0]);
        var areaColor = new dojo.Color([220,220,0,0.5]);
        var areaOutline = new esri.symbol.SimpleLineSymbol(areaOutlineStyle, areaOutlineColor, 2);
        var areaSymbol = new esri.symbol.SimpleFillSymbol(areaStyle, areaOutline, areaColor);
        feature.setSymbol(areaSymbol);

          //Construct infoWindow
        var attr = feature.attributes;
        var title = "<center>PA Landforms</center>";
        var content = "<font face='tahoma' color='red'><b>Landform Region:  </b></font>"+attr.PROVINCE + "<br /><font face='tahoma' color='red'><b>Subregion:  </b></font>"+attr.SECTION;
        map.graphics.add(feature);
       
        map.infoWindow.resize(260,90);
        map.infoWindow.setTitle(title);
        map.infoWindow.setContent(content);
       
        map.infoWindow.show(evt.screenPoint,map.getInfoWindowAnchor(evt.screenPoint));
      }
     
      function execute(searchText) {
        //Set the search text
        findParams.searchText = searchText;
        findPlace.execute(findParams, showResults);
      }
     
      function showResults(results) {
        //Set symbology
        window.alert("Im here");
        var pointStyle = esri.symbol.SimpleMarkerSymbol.STYLE_CIRCLE;
        var pointOutlineStyle = esri.symbol.SimpleLineSymbol.STYLE_SOLID;
        var symbolColor = new dojo.Color([255,0,0]);
        var lineSymbol = new esri.symbol.SimpleLineSymbol(pointOutlineStyle,symbolColor,1);
        var pointSymbol = new esri.symbol.SimpleMarkerSymbol(pointStyle,10,lineSymbol,symbolColor);
       
        //find results returns an array of findResult
        map.graphics.clear();
        var dataForSymbol = [];
       
        //Build an array and add each found place to the map
        dojo.forEach(results, function(result) {
        window.alert(result.layerName);
          var graphic = result.feature;
          dataForSymbol.push([result.layerName, result.foundFieldName, result.value]);
              graphic.setSymbol(pointSymbol);
          map.graphics.add(graphic);
        });
      }
     
      dojo.addOnLoad(init);

    </script>
  </head>
 
  <body class="claro">
    <div id="content" dojoType="dijit.layout.BorderContainer" design="headline" gutters="true" style="width: 100%; height: 100%; margin: 0;">
      <div class="searches" dojotype="dijit.layout.BorderContainer" region="top" style="height:30px;">
        <h1>Pennsylvania Landform Finder</h1>
      </div>
      <div id="legendPane" dojoType="dijit.layout.ContentPane" title="Legend" selected="true" region="right">
        <div id="legendDiv">
          <h3>Place Name:</h3>
          <input type="text" id="searchText" value="Philadelphia" /><br />
          <input type="button" value="Find It" onclick="execute(dojo.byId('searchText').value);"/><br />
          <p><h4>To find the landform region in which a place is located, type the name of the place in the box
          above and click on "Find It."  When the place is highlighted, zoom the map closer to it, and then
          click on the place; the landform area surrounding it will be highlighted and the identifying information will pop up.</h4></p> 
        </div>
      </div>
      <div id="mapDiv" dojoType="dijit.layout.ContentPane" region="center" style="overflow:hidden;"></div>
    </div>
  </body>
0 Kudos
5 Replies
derekswingley1
Deactivated User
The findTask runs against a map service and you specify which layers you'd like to search using findParameters.layerIds.

Try using your map service URL as opposed to the URL for a specific layer in the findTask constructor.
0 Kudos
CharlesGeiger
Regular Contributor
Thanks for your quick response, Derek.  I am using 3 map services: the Esri World_Terrain_Base, my own PA_Landforms regions and subregions (2 feature classes in one map service), and my own PAplaces for cities/towns (as points, layerID=0) and cities (as polygons, layerID=1).  I think I have the map service URL for the PAplaces map service specified correctly.

I apologize for not using the HTML code tags, but I'm not sure how to do that.
0 Kudos
derekswingley1
Deactivated User
Hi Charles,

No worries about the code tags...it's just a suggestion.

Regarding your code, try these two things first:

  1. do not add an additional slash on the map service URL when creating your find task

  2. specify layerIds on your findParameters


Point number one isn't breaking anything, but that slash isn't necessary. Point number two can cause queries to fail, see this URL as an example:  https://mapmaker.millersville.edu/ArcGIS/rest/services/PAplaces/MapServer/find?f=json&searchText=Phi...
0 Kudos
CharlesGeiger
Regular Contributor
That's an improvement!

I had to change:
        findParams.layerIDs = [0];
to
        findParams.layerIds = [0];

It still doesn't add the graphic, perhaps because of how I adapted the sample code.  Theirs allowed the findTask to collect points, lines and areas with the same searchText name; mine has only point locations.  Do you see any issues with the block that begins with:
        dojo.forEach(results, function(result) {
0 Kudos
derekswingley1
Deactivated User
Nothing jumps out at me...but if your getting features back now it's probably something with your symbol. Try adding graphics using the default symbology options:

graphic.setSymbol(new esri.symbol.SimpleMarkerSymbol());
map.graphics.add(graphic);
0 Kudos