Select to view content in your preferred language

QueryTask and Zoom to Result

7030
11
02-18-2013 05:47 AM
MJWaldstein
New Contributor
Hi,

I am looking to see if anyone knows how I can have both a queryTask function and have the map zoom to the result automatically after the search button is clicked. I am stuck on how to create the zoom function, as my search works.

Below is the code i have for the search ability:

var watershed_areas = new esri.layers.ArcGISDynamicMapServiceLayer("http://54.245..................");
map.addLayers(watershed_areas);



findTask = new esri.tasks.FindTask("http://54.245...............");

   
    findParams = new esri.tasks.FindParameters();
    findParams.returnGeometry = true;
    findParams.layerIds = [0];
    findParams.searchFields = ["NWWTRSHDCD", "WTRSHDD"];


   

function execute(searchText) {
   
    findParams.searchText = searchText;
    findTask.execute(findParams, showResults);
   }
   function showResults(results) {   
    var polygonSymbol = new esri.symbol.SimpleFillSymbol(esri.symbol.SimpleFillSymbol.STYLE_SOLID, new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([255, 0, 0]), 1), new dojo.Color([255, 0, 0, 1]));
    map.graphics.clear();
    var dataForGrid = [];
   
    dojo.forEach(results, function(result) {
     var graphic = result.feature;
     dataForGrid.push([result.layerName, result.foundFieldName, result.value]);
     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);
    });
    var data = {
     items : dataForGrid
    };
    var store = new dojo.data.ItemFileReadStore({
     data : data
    });
    grid.setStore(store);
   }

If anyone could lend any assistance, i would greatly appreciate it.

Thanks,

Margaret
0 Kudos
11 Replies
WillHughes1
Frequent Contributor
Thanks Ken.

I just tried using the centerAndZoom method and it worked perfectly.
map.centerAndZoom(resultFeatures[0].geometry, .01);


Will
0 Kudos
KenBuja
MVP Esteemed Contributor
Thanks Ken.

I just tried using the centerAndZoom method and it worked perfectly.
map.centerAndZoom(resultFeatures[0].geometry, .01);


Will


You should check what happens when you try this multiple times to see if it continues to zoom in.

And when you find posts useful, made good use of the "This post was helpful" up-arrow 🙂
0 Kudos