Zoom to Graphic?

2783
3
08-03-2011 09:04 AM
JeremyAdams
New Contributor
Is it possible to zoom to a graphic? 

My Application allows a user to enter a Parcel Number and use the findTask to create a graphic over the correct Parcel on the map.  But there are a lot of parcels so I would like to be able to zoom to the graphic.  Is this possible?

I've looked at centerAndZoom(point, lvl).  But I cant seem to figure out how to get a point from my graphic because its as Polygon not a Point.

Thanks for the help in Advance.

      //Executed to search for a Parcel with the correct Map and Page number
      function findParcel() { 
        //set the search text to find parameters 
        var searchText = dojo.byId("Map").value;
        if (searchText.length == 4) {
          searchText = searchText + "  " + dojo.byId("Page").value;
        }
        else if (searchText.length == 3 ){          
          searchText = searchText + "   " + dojo.byId("Page").value;         
        }
        findParams.searchText = searchText; 
        findTask.execute(findParams, showParcel); 
      } 
            
     function showParcel(results) { 
        //symbology for graphics 
        var polygonSymbol = new esri.symbol.SimpleFillSymbol(esri.symbol.SimpleFillSymbol.STYLE_SOLID, new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_DASHDOT, new dojo.Color([255, 0, 0]), 2), new dojo.Color([255, 0, 0, 0.5])); 
 
        //find results return an array of findResult. 
        map.graphics.clear(); 
        //Build an array of attribute information and add each found graphic to the map 
        dojo.forEach(results, function(result) { 
          var graphic = new esri.Graphic(result.feature); 
            graphic.setSymbol(polygonSymbol); 
          map.graphics.add(graphic);
        });            
      } 
0 Kudos
3 Replies
derekswingley1
Frequent Contributor
Yes, check out esri.graphicsExtent().

If your callback, pass your array of graphics to esri.graphicsExtent(), save the return value and then set your map extent using map.setExtent().
0 Kudos
JeremyAdams
New Contributor
Thanks,

I tried that, but it seems to stop execution or break out of the showresults() when the new extent is created:        var parcelExtent = esri.graphicsExtent(results.features);

The application continues to function, but the graphic is never added to the map now.  I know I've got something wrong I just can see it.

     function showParcel(results) { 
        //symbology for graphics 
        var polygonSymbol = new esri.symbol.SimpleFillSymbol(esri.symbol.SimpleFillSymbol.STYLE_SOLID, new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_DASHDOT, new dojo.Color([255, 0, 0]), 2), new dojo.Color([255, 0, 0, 0.5])); 
        
        //create an extent matching the graphics of the parcel(s)
        var parcelExtent = esri.graphicsExtent(results.features);
        
        //find results return an array of findResult. 
        map.graphics.clear(); 
        //Build an array of attribute information and add each found graphic to the map 
        dojo.forEach(results, function(result) { 
          var graphic = new esri.Graphic(result.feature); 
            graphic.setSymbol(polygonSymbol); 
          map.graphics.add(graphic);
        });
              
        map.setExtent(parcelExtent);
          
      } 
0 Kudos
JeremyAdams
New Contributor
I have found the way to fix the zoom to graphic problem  I had to create a graphics layer and the pass the array in it to the zoomExtent() fcn.   This works great because I can zoom to a single or multiple parcels depending on input.

     function showParcel(results) { 
        //symbology for graphics 
        var polygonSymbol = new esri.symbol.SimpleFillSymbol(esri.symbol.SimpleFillSymbol.STYLE_SOLID, new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_DASHDOT, new dojo.Color([255, 0, 0]), 2), new dojo.Color([255, 0, 0, 0.5])); 
       
        //find results return an array of graphics in the graphicslayer.
        var graphicslayer = new esri.layers.GraphicsLayer();        
        map.graphics.clear(); 
        //Build an array of attribute information and add each found graphic to the map 
        dojo.forEach(results, function(result) { 
          var graphic = new esri.Graphic(result.feature); 
          graphic.setSymbol(polygonSymbol); 
          map.graphics.add(graphic);  
          graphicslayer.add(graphic);                          
        });

        //create an extent matching the graphics of the parcel(s)
        var parcelExtent = esri.graphicsExtent(graphicslayer.graphics);      
        map.setExtent(parcelExtent);          
      } 
0 Kudos