Cannot zoom to graphic

996
5
03-11-2014 11:35 AM
williamcarr
Occasional Contributor II
I've been tackling this problem all day and help would surely be appreciated. I'm new to JS and I'm attempting to zoom to a graphic selected from a  FindTask. The graphic shows up on the correct point but I can't for the life of me get it to zoom to a point feature. Thanks

This is slapped together from various samples and examples. JS 3.8.


 //create find parameters

        findParams = new esri.tasks.FindParameters();
        findParams.returnGeometry = true;
        findParams.layerIds = [0];
        findParams.searchFields = ["OBJECTID"];
        var sr = new esri.SpatialReference({wkid:102100});
        findParams.outSpatialReference = sr;
      }

      //set the search text to find parameters
      function execute(searchText) {
        findParams.searchText = searchText;
        findTask.execute(findParams, showResults);
      }

      //symbology for graphics
      function showResults(results) {
        var markerSymbol = new esri.symbol.SimpleMarkerSymbol(esri.symbol.SimpleMarkerSymbol.STYLE_SQUARE, 10);
        
      //find results return an array of findResult.
        map.graphics.clear();
        var dataForGrid = [];

        //Build an array of attribute information and add each found graphic to the map
        dojo.forEach(results, function(result) {
          var graphic = result.feature;

          if (graphic.geometry.x === 0 || graphic.geometry.y === 0) {
            console.warn(graphic.geometry.toJson());
            return;
          }

          switch (graphic.geometry.type) {
            case "point":
              graphic.setSymbol(markerSymbol);
              break;       
          }
      
          //add graphics to map
          map.graphics.add(graphic);
       
       
         map.setExtent(map.graphic[0].feature.geometry.getExtent());
    
        //THIS IS WHERE EVERYTHING GOES WRONG.....
        if (map.graphics.graphic.length > 1) {
          var graExtent = esri.graphicsExtent(map.graphics.graphics); 
           map.setExtent(map.graphic[0].feature.geometry.getExtent());
        }
        else if (map.graphics.graphic.count == 1) {
          map.setExtent(map.graphic[0].feature.geometry.getExtent());
        }
         }); 
      }

      dojo.addOnLoad(init);
0 Kudos
5 Replies
KenBuja
MVP Esteemed Contributor
A single point essentially does not have an extent. Take a look at this thread which shows how to zoom to a single point.
0 Kudos
williamcarr
Occasional Contributor II
Thanks for the reply, Ken.

I'm still not getting any luck using extents. Would it be easier to to use a centerAt or a centerAndZoom?
0 Kudos
williamcarr
Occasional Contributor II
I got it fixed.

 //add graphics to map
           map.graphics.clear();
          map.graphics.add(graphic);
       map.centerAndZoom(graphic.geometry, 15);


I think this will only handle single feature graphics from a query, but it is exactly what I needed.
0 Kudos
sumitzarkar
Occasional Contributor
If you have multiple features then you need to add those features to Graphicslayer and get the FullExtent of that GraphicsLayer and set that extent to map.
0 Kudos
KenBuja
MVP Esteemed Contributor
William, you should mark your post as the answer to help those who are searching for a solution to this question.
0 Kudos