Select to view content in your preferred language

Zoom to the full extent of a Query task in C#

3230
1
07-18-2012 11:39 PM
DavidGeorge4
Emerging Contributor
Hi,

I am have a little trouble trying to get the map to zoom to full extent of a query task using C#.

The code i am using is as follows:

       
        QueryTask queryTask;
                queryTask = new QueryTask("http://fcvmsql85/ArcGIS/rest/services/Lancs_AE_Activity_Total_Cost/MapServer/1");

                queryTask.ExecuteCompleted += QueryTask_ExecuteCompleted;
                queryTask.Failed += QueryTask_Failed;


                ESRI.ArcGIS.Client.Tasks.Query query = new ESRI.ArcGIS.Client.Tasks.Query();

                query.Where = "GPP_Code in (" + GPP_Code + ") AND f_year = " + f_year;
                query.ReturnGeometry = true;
                queryTask.ExecuteAsync(query);

            }
            catch (Exception Ex)
            {
                MessageBox.Show("Page Failed: " + Ex.Message.ToString());
            }
        }

        private void QueryTask_ExecuteCompleted(Object sender , ESRI.ArcGIS.Client.Tasks.QueryEventArgs args)
        {
            FeatureSet featureSet;
        featureSet = args.FeatureSet;

        if( featureSet.Features.Count > 0 )
            {
            
              ESRI.ArcGIS.Client.Geometry.Envelope selectedFeatureExtent= featureSet.Features [0].Geometry.Extent;
            // Increase Extent by value
            Int32  expandVal = 500;

            ESRI.ArcGIS.Client.Geometry.Envelope displayExtent = new  ESRI.ArcGIS.Client.Geometry.Envelope();
            displayExtent.XMin = selectedFeatureExtent.XMin - expandVal;
            displayExtent.YMin = selectedFeatureExtent.YMin - expandVal;
            displayExtent.XMax = selectedFeatureExtent.XMax + expandVal;
            displayExtent.YMax = selectedFeatureExtent.YMax + expandVal;
           
            MyMap1.ZoomTo(displayExtent);

           
            }
            else
            {
                MessageBox.Show("No f returned from query");
            }
        }



However, this code only allows me to set the extent of a specific id from the query task (eg [0])

The section of code where is specified is here:

ESRI.ArcGIS.Client.Geometry.Envelope selectedFeatureExtent= featureSet.Features [0].Geometry.Extent;

What i need is the a zoom to the extent of all  of the features returned in the query task and not just ID's that are specified in the code.


I suspect there is something obvious that I am missing but cant quite think what.

Any advise would be extremely welcome!

Thanks

Dave
0 Kudos
1 Reply
JoeHershman
MVP Alum
One way that should work would be to create a GraphicsLayer and add all the features to it


        private void QueryTask_ExecuteCompleted(Object sender, ESRI.ArcGIS.Client.Tasks.QueryEventArgs args)
        {
            FeatureSet featureSet;
            featureSet = args.FeatureSet;
            GraphicsLayer graphicsLayer = new GraphicsLayer();


            if ( featureSet.Features.Count > 0 )
            {
                graphicsLayer.GraphicsSource = featureSet.Features;
                MyMap1.ZoomTo(graphicsLayer.FullExtent);
            }
            else
            {
                MessageBox.Show("No f returned from query");
            }
        }



I think this would still work even though the GraphicsLayer is not added to the map
Thanks,
-Joe
0 Kudos