Select to view content in your preferred language

ArcGIS JavaScript API: Zoom to point feature using FindTask

2556
6
Jump to solution
10-01-2014 03:11 AM
HabG_
by
Frequent Contributor

Hi All,

I am using the FindTask in my web map (Note: I have point and polygon layers) and it is working fine. When I search using a keyword from the polygon layer it zooms to the polygon and selects the polygon/s using graphics which is what I wanted. However, when I search using a keyword from the point layer, it pans to the center of the selected point/s and doesn't show any graphics. So what I want is, when I search using a point key word, to zoom to the selected point/s and show a circle or square graphics. Below is the code I'm using (Note: I have changed my server name to "myserver:6080").

Thanks.

findTask = new FindTask("http://myserver:6080/arcgis/rest/services/gisdata/webmap/MapServer/");  

     

      map.on("load", function () {  

          //Create the find parameters  

          findParams = new FindParameters();  

          findParams.returnGeometry = true;  

          findParams.layerIds = [0, 1, 2];  

          findParams.searchFields = ["IDNO", "RESID", "INT", "FTNM"];  

          findParams.outSpatialReference = map.spatialReference;  

          console.log("find sr: ", findParams.outSpatialReference);  

        });

        function doFind() {  

          //Set the search text to the value in the box  

          findParams.searchText = dom.byId("SEARCH").value;  

          findTask.execute(findParams, showResults);  

        }

        function showResults(results) {  

          //This function works with an array of FindResult that the task returns

        var markerSymbol = new SimpleMarkerSymbol(SimpleMarkerSymbol.STYLE_CIRCLE, 10, new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID, new Color([255, 0, 0]), 1.5), new Color([255, 0, 0, 0.5]));

        var polygonSymbol = new SimpleFillSymbol(SimpleFillSymbol.STYLE_SOLID, new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID, new Color([255, 0, 0]), 2), new Color([255, 0, 0, 0.5]));

          //create array of attributes  

          var items = arrayUtils.map(results, function (result) {  

            var graphic = result.feature;  

            graphic.setSymbol(markerSymbol);

            graphic.setSymbol(polygonSymbol);

            map.graphics.add(graphic);  

            return result.feature; 

          });

          var myFeatureExtent = graphicsUtils.graphicsExtent(items); 

          map.setExtent(myFeatureExtent, true);

        }

0 Kudos
1 Solution

Accepted Solutions
HabG_
by
Frequent Contributor

Hi Jake, I changed the second "if" statement to "else" and now I'm able to see the graphics in both point and polygon features. Thanks for your help.

View solution in original post

0 Kudos
6 Replies
OwenEarley
Frequent Contributor

It seems you are overwriting your point symbol with your polygon symbol:

var graphic = result.feature;

graphic.setSymbol(markerSymbol); 

graphic.setSymbol(polygonSymbol);  // this will replace the previous symbol

map.graphics.add(graphic);

You should test the geometry type before setting the appropriate symbol.

0 Kudos
HabG_
by
Frequent Contributor

Thanks for your reply Owen. How do I test the geometry type?

0 Kudos
JakeSkinner
Esri Esteemed Contributor

Hi Hab,

You could do something like below:

var items = arrayUtils.map(results, function (result) {

  var graphic = result.feature;

  if(graphic.geometry.type == 'polygon'){

    graphic.setSymbol(polygonSymbol);   

  };   

  if(graphic.geometry.type == 'point'){   

    graphic.setSymbol(markerSymbol);

  } 

   

  map.graphics.add(graphic);    

  return result.feature;   

});

HabG_
by
Frequent Contributor

Hi Jake, the graphics is now showing on the point feature not on the polygon feature.

0 Kudos
HabG_
by
Frequent Contributor

Hi Jake, I changed the second "if" statement to "else" and now I'm able to see the graphics in both point and polygon features. Thanks for your help.

0 Kudos
JakeSkinner
Esri Esteemed Contributor

Good catch.  That was a type-o on my part.

0 Kudos