zoom to found features

1608
4
06-22-2012 10:33 AM
Jeanlamou
New Contributor
I would like to zoom on found features (through esri:FindTask, or esri:QueryTask). It works fine when the number of found features is more than one. If the number is one, there is an error indicating that it cannot access properties of null object.

I am using Arcgis Api for Flex 2.5. For the esri:QueryTask, I use "graphicsExtent:Extent = GraphicUtil.getGraphicsExtent(featureSet.features);". For FindTask, I used the sample: http://help.arcgis.com/en/webapi/flex/samples/index.html#/Zoom_to_found_features/01nq0000005n000000/

Any idea what's wrong? Thanks

Jean
Tags (2)
0 Kudos
4 Replies
FengZhang2
Occasional Contributor
Are you trying to zoom to a single point feature? If it is, please check the following sample code for zooming to FindTask Results:

            
            private function executeCompleteHandler(event:FindEvent):void
            {
                myGraphicsLayer.clear();
                resultSummary.text = "Found " + event.findResults.length + " results.";
                myGraphicsLayer.symbol = sfsFind;
                var resultCount:int = event.findResults.length;
                
                var graphic:Graphic;
                
                // check number of results
                if (resultCount == 0)
                {
                    Alert.show("No Results Found.");
                }
                // for one point results
                else if (resultCount == 1)
                {
                 graphic = event.findResults[0].feature;
                 var mapPoint:MapPoint = MapPoint(graphic.geometry);
                 graphic.symbol = sms;
                 map.centerAt(mapPoint);
                 map.scale = 1200;
                }
                // more than 1 point = MultiPoint
                else if (resultCount > 1)
                {
          // create a new MultiPoint to add all our point results to
                   var multi:Multipoint = new Multipoint();
                    // add feature as graphic to graphics layer
                    for (var i:Number = 0; i < resultCount; i++)
                    {
                        graphic = event.findResults.feature;
                        graphic.toolTip =  event.findResults.foundFieldName + ": " + event.findResults.value;
                        var multiMapPoint:MapPoint = MapPoint(graphic.geometry);
                        multi.addPoint(multiMapPoint);
                        myGraphicsLayer.add(graphic);
                    }
                    
                    // zoom to extent of the multipoint
                    var nExt:Extent = multi.extent.expand(1.25);
        map.extent = nExt;
                }
            }

0 Kudos
Jeanlamou
New Contributor
Perfect, it works. Thanks Feng.

Jean
0 Kudos
SRIKANTHSwarna
New Contributor
Hi,
    How to zoom to Multiple polygons on the map from results, I was able to do zoom to  multiple points.here is the code i used.

code:
function results(results){
  var resultFeatures = results.features;
  var points =  new esri.geometry.Multipoint(map.spatialReference);
  for (var i=0, il = resultFeatures.length; i<il; i++) {
   if(results.features.geometry.type=="point"){
//IF CONDITION FOR MULTIPLE POINTS ZOOM
    var pointx;
    var pointy;
    pointx = results.features.geometry.x; //webmercator geometry
    pointy = results.features.geometry.y;
    point = new esri.geometry.Point(pointx, pointy,globals.map.spatialReference);                             if(resultFeatures.length=='1'){
    
    map.centerAndZoom((point),13);
    }
     
                 else{
     points.addPoint(point);
    
    }
    //globals.map.graphics.add(new esri.Graphic(point, symbol));
    map.graphics.add(new esri.Graphic(point, getZoomSymbol())); 
   }
   else{
//CODE FOR ZOOM TO MULTIPLE POLYGONS
    ext = results.features.geometry.getExtent();
    //results.features.setSymbol(symbol);
    results.features.setSymbol(getZoomSymbol());
    map.graphics.add(results.features);
    var factor = 0;
    if (extent!=null) {   
     extent = extent.union(ext);
    } else {
     extent = ext;
    }
    map.setExtent(extent);
   }
  }
  if(points.points.length>=1){
  .map.setExtent(points.getExtent().expand(3));
  } }

zoom to multiple points is working fine, but for multiple polygons is not working, I mean it is going to zoom to one polygon only, i can't set the extent to see all the polygons on the map.



Thanks,
SS
0 Kudos
RobertScheitlin__GISP
MVP Emeritus
SS,

   You need to look into using the GraphicsUtil class it has a getGraphicsExtent function:

http://resources.arcgis.com/en/help/flex-api/apiref/com/esri/ags/utils/GraphicUtil.html
0 Kudos