AnsweredAssumed Answered

FindTask - zoom to results not working (multiple layers returning features)

Question asked by Jenn_88 on Feb 4, 2015
Latest reply on Feb 5, 2015 by Jenn_88

Hi everyone,

 

I've been working on a web map and am having trouble zooming to the results of the FindTask. At some point, I'll also need to incorporate the functionality to zoom to a result if it's a single point, but first I'm just trying to get it to show results from a line and polygon layer! If someone could help out with my conundrum that would be lovely. Thank you!

 

<script>
 // get the job number from the URL
 var projNum = getUrlParameter('data').split("+")[0]; //this is a call to a function in a helper file
 
    require([
  "esri/map",
        "esri/tasks/FindTask",
        "esri/tasks/FindParameters",
        "esri/symbols/SimpleMarkerSymbol",
        "esri/symbols/SimpleLineSymbol",
        "esri/symbols/SimpleFillSymbol",
          
        "esri/Color",
        "dojo/on",
        "dojo/dom",
        "dijit/registry",
        "dojo/_base/array",
        "dojo/_base/connect",
        "dojo/parser",
          
        "dijit/layout/BorderContainer",
        "dijit/layout/ContentPane",
        "dojo/domReady!"
    ], function(
        Map, FindTask, FindParameters, SimpleMarkerSymbol, SimpleLineSymbol, SimpleFillSymbol,
        Color, on, dom, registry, arrayUtils, connect, parser
    ) {      
        var findTask, findParams;
        var map, center, zoom;
        
        parser.parse();        
        map = new esri.Map("map", {
   basemap: "streets",
   center: [-122.8, 49.2],
   zoom: 10
        });
        //Create Find Task using the URL of the map service to search
        findTask = new FindTask("URL HERE");
        map.on("load", function () {  
   //Create the find parameters
   findParams = new FindParameters();
   findParams.returnGeometry = true;
   findParams.layerIds = [0,1,2];
   findParams.searchFields = ["Project_Number"];
   findParams.outSpatialReference = map.spatialReference;
   console.log("find sr: ", findParams.outSpatialReference);
   
   //Set the search text to the value in the box
   findParams.searchText = projNum;
   findTask.execute(findParams, showResults);   
        });                  
        function showResults(results) {
   //This function works with an array of FindResult that the task returns
   map.graphics.clear();
    
   //SYMBOLOGY FOR GRAPHICS
   //polygon
   var polygonSymbol = new SimpleFillSymbol(
    SimpleFillSymbol.STYLE_SOLID, 
    new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID, new Color([0, 0, 0]), 1), 
    new Color([98, 194, 204, 0.5])
   );
    
   //line
   var lineSymbol = new SimpleLineSymbol();
    //SimpleLineSymbol.STYLE_SOLID;
    lineSymbol.setWidth(3);
    lineSymbol.setColor(new Color([0,230,169,1]));
   
   //point
   var markerSymbol = new SimpleMarkerSymbol();
    markerSymbol.setSize(12);
    markerSymbol.setColor(new Color([0,230,169,1]));          
   //create array of attributes
   var items = arrayUtils.map(results, function (result) {
    var graphic = result.feature;
    switch (graphic.geometry.type) {
     case "point":
      graphic.setSymbol(markerSymbol);
      break;
     case "polyline":
      graphic.setSymbol(lineSymbol);
      break;
     case "polygon":
      graphic.setSymbol(polygonSymbol);
      break;   
    }
         map.graphics.add(graphic);   
            return result.feature; 
        });
  var myFeatureExtent = graphicsUtils.graphicsExtent(items);    
  map.setExtent(myFeatureExtent, true);
        } 
    });      
    </script>

Outcomes