cgeiger

Beginner: findTask does not seem to work

Discussion created by cgeiger on Sep 23, 2011
Latest reply on Sep 23, 2011 by DSwingley-esristaff
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>

Outcomes