Zoom To Point

6052
8
01-25-2011 10:28 AM
NathalieNeagle
New Contributor III
I know this subject has been beat to death and you can't zoom to a point like you do a line or poly but I'm having trouble.

So if I'm suppose to use this:

 Map.ZoomToResolution(Map.Resolution / 2, pointToZoomTo);




And Currently have this from the SDK


if (featureSet != null && featureSet.Features.Count > 0)

                foreach (Graphic feature in featureSet.Features)
                {
                    // Hightlight selected feature
                    feature.Symbol = LayoutRoot.Resources["ResultsMarkerSymbol"] as SimpleMarkerSymbol;

                    AGraphicslayer.Graphics.Insert(0, feature);

                    // Zoom to selected feature (define expand percentage)

                     
                
                    ESRI.ArcGIS.Client.Geometry.Envelope selectedFeatureExtent = feature.Geometry.Extent;

                    double expandPercentage = 30;

                    double widthExpand = selectedFeatureExtent.Width * (expandPercentage / 100);
                    double heightExpand = selectedFeatureExtent.Height * (expandPercentage / 100);

                    ESRI.ArcGIS.Client.Geometry.Envelope displayExtent = new ESRI.ArcGIS.Client.Geometry.Envelope(
                    selectedFeatureExtent.XMin - (widthExpand / 2),
                    selectedFeatureExtent.YMin - (heightExpand / 2),
                    selectedFeatureExtent.XMax + (widthExpand / 2),
                    selectedFeatureExtent.YMax + (heightExpand / 2));

                    Map.ZoomTo(displayExtent);

                   



What do I need to change the "pointToZoomTo" parameter to, I've tried AGraphicslayer and featureSet but I'm just unsure and nothing is working

  Map.ZoomToResolution(Map.Resolution / 2, pointToZoomTo);


Thanks
Nathalie
0 Kudos
8 Replies
JenniferNery
Esri Regular Contributor
Is pointToZoom already a MapPoint?

If you wish to ZoomToResolution and also re-center the map, you can use the overload that accepts MapPoint http://help.arcgis.com/en/webapi/silverlight/apiref/ESRI.ArcGIS.Client~ESRI.ArcGIS.Client.Map~ZoomTo...

Post #6 in this thread http://forums.arcgis.com/threads/13749-how-to-Zoom-to-point talks about creating an Envelope around your MapPoint.
0 Kudos
dotMorten_esri
Esri Notable Contributor
Map.ZoomToResolution does NOT zoom to the point you specify. It zooms "around" that point. Tthis is basically what happens when you mousewheel or dblclick. The point you clicked stays at the same place and everything else moves around it (try this in the corner of the map, and it will be obvious that it doesn't center on this point, but instead keeps that point under the mouse cursor).

If you want to zoom to a point, create an Envelope that is centered on the point,  and zoom to that.
Ie. Center on MapPoint 'p' with 10 map units margin around it:
MyMap.ZoomTo(new Envelope(p.X-10,p.Y-10,p.X+10,p.Y+10);
0 Kudos
NathalieNeagle
New Contributor III
Jennifer and Morton,

Thanks for the reply.  O.K. I'm sorry for my ignorance but I'm still having the same issue.  How do assign "p" to the queried/selected graphics?

I now understand I should use this code:
Envelope env = new Envelope(p.X - 500, p.Y - 500, p.X + 500, p.Y + 500);
                    Map.ZoomTo(env);


But what do I set the variable "p" to and how? I've tried to define "p" as:
ESRI.ArcGIS.Client.Geometry.MapPoint p = AGraphicslayer;


or

ESRI.ArcGIS.Client.Geometry.MapPoint p = feature;


But I can't convert MapPoint to Graphic or feature so if this is my attribute selection code and the one graphic that is being selected based on the users query is what I want to zoom to then how do set the graphic to the MapPoint "p"

 private void ExcuteMS4Query_Click(object sender, RoutedEventArgs e)
        {
            //Define query task and when query is sucessful fire off AQueryTask_ExecuteCompleted
            QueryTask queryTask = new QueryTask("http://arcgisserver10/ArcGIS/rest/services/MS4/MapServer/0");
            queryTask.ExecuteCompleted += MS4QueryTask_ExecuteCompleted;
            queryTask.Failed += MS4QueryTask_Failed;

            GraphicsLayer selectionGraphicslayer = Map.Layers["MySelectionGraphicsLayer"] as GraphicsLayer;
            selectionGraphicslayer.ClearGraphics();

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

            query.Where = string.Format("OBJECTID = '{0}'", FindMS4.Text);

            query.OutFields.Add("*");

            query.Text = FindMS4.Text;

            //If query is sucessful fire off AQueryTask_ExecuteCompleted
            queryTask.ExecuteAsync(query);
        }


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

            GraphicsLayer AGraphicslayer = Map.Layers["MySelectionGraphicsLayer"] as GraphicsLayer;

           


            if (featureSet != null && featureSet.Features.Count > 0)

                foreach (Graphic feature in featureSet.Features)
                {
                    // Hightlight selected feature
                    feature.Symbol = LayoutRoot.Resources["ResultsMarkerSymbol"] as SimpleMarkerSymbol;

                    AGraphicslayer.Graphics.Insert(0, feature);

                    // Zoom to selected feature (define expand percentage)
          //This zoom to selected feature doesn't work for points so I will comment
         //it out at a later date after I figure out new zoom point protocal 

                     
                
                    ESRI.ArcGIS.Client.Geometry.Envelope selectedFeatureExtent = feature.Geometry.Extent;

                    double expandPercentage = 30;

                    double widthExpand = selectedFeatureExtent.Width * (expandPercentage / 100);
                    double heightExpand = selectedFeatureExtent.Height * (expandPercentage / 100);

                    ESRI.ArcGIS.Client.Geometry.Envelope displayExtent = new ESRI.ArcGIS.Client.Geometry.Envelope(
                    selectedFeatureExtent.XMin - (widthExpand / 2),
                    selectedFeatureExtent.YMin - (heightExpand / 2),
                    selectedFeatureExtent.XMax + (widthExpand / 2),
                    selectedFeatureExtent.YMax + (heightExpand / 2));

                    Map.ZoomTo(displayExtent);

                   // Map.ZoomToResolution(Map.Resolution / 2, );


                   
                    // ResultsDisplay.Visibility = Visibility.Visible;
                    ResultsDisplay.IsExpanded = true;

                }
            _drawSurface.IsEnabled = false;

        }


      
        private void MS4QueryTask_Failed(object sender, TaskFailedEventArgs args)
        {
            MessageBox.Show("Query failed: " + args.Error);
        }




Thanks a bunch.
0 Kudos
NathalieNeagle
New Contributor III
I'm still having a problem figuring out how to assign "p" to the queried/selected graphics?
Would someone be so kind and help me out?
Thanks
Nathalie
0 Kudos
dotMorten_esri
Esri Notable Contributor
'p' is the point you wanted to zoom to in the first place.
0 Kudos
NathalieNeagle
New Contributor III
Morton I understand that p is the point I want to zoom to but p is not defined in my code.  I'm using a simple query to query a point and select it.  I don't have p defined and don't know how to set it to my selected graphic.

I'm sure this is super elementary but....I'm just not there yet.


 private void ExcuteMS4Query_Click(object sender, RoutedEventArgs e)
        {
            //Define query task and when query is sucessful fire off AQueryTask_ExecuteCompleted
            QueryTask queryTask = new QueryTask("http://arcgisserver10/ArcGIS/rest/services/MS4/MapServer/0");
            queryTask.ExecuteCompleted += MS4QueryTask_ExecuteCompleted;
            queryTask.Failed += MS4QueryTask_Failed;

            GraphicsLayer selectionGraphicslayer = Map.Layers["MySelectionGraphicsLayer"] as GraphicsLayer;
            selectionGraphicslayer.ClearGraphics();

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

            query.Where = string.Format("OBJECTID = '{0}'", FindMS4.Text);

            query.OutFields.Add("*");

            query.Text = FindMS4.Text;

            //If query is sucessful fire off AQueryTask_ExecuteCompleted
            queryTask.ExecuteAsync(query);
        }


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

            GraphicsLayer AGraphicslayer = Map.Layers["MySelectionGraphicsLayer"] as GraphicsLayer;

           


            if (featureSet != null && featureSet.Features.Count > 0)

                foreach (Graphic feature in featureSet.Features)
                {
                    // Hightlight selected feature
                    feature.Symbol = LayoutRoot.Resources["ResultsMarkerSymbol"] as SimpleMarkerSymbol;

                    AGraphicslayer.Graphics.Insert(0, feature);

                    // Zoom to selected feature (define expand percentage)
          //This zoom to selected feature doesn't work for points so I will comment
         //it out at a later date after I figure out new zoom point protocal 

                     
                
                    ESRI.ArcGIS.Client.Geometry.Envelope selectedFeatureExtent = feature.Geometry.Extent;

                    double expandPercentage = 30;

                    double widthExpand = selectedFeatureExtent.Width * (expandPercentage / 100);
                    double heightExpand = selectedFeatureExtent.Height * (expandPercentage / 100);

                    ESRI.ArcGIS.Client.Geometry.Envelope displayExtent = new ESRI.ArcGIS.Client.Geometry.Envelope(
                    selectedFeatureExtent.XMin - (widthExpand / 2),
                    selectedFeatureExtent.YMin - (heightExpand / 2),
                    selectedFeatureExtent.XMax + (widthExpand / 2),
                    selectedFeatureExtent.YMax + (heightExpand / 2));

                    Map.ZoomTo(displayExtent);

                   // Map.ZoomToResolution(Map.Resolution / 2, );


                   
                    // ResultsDisplay.Visibility = Visibility.Visible;
                    ResultsDisplay.IsExpanded = true;

                }
            _drawSurface.IsEnabled = false;

        }


      
        private void MS4QueryTask_Failed(object sender, TaskFailedEventArgs args)
        {
            MessageBox.Show("Query failed: " + args.Error);
        }
0 Kudos
dotMorten_esri
Esri Notable Contributor
"I'm using a simple query to query a point and select it."

That "point" you are referring to is your 'p', whatever you named that. Just replace my 'p' with the name of your point.
0 Kudos
SergioMoreno_Rojas
New Contributor
selectedFeature.Symbol = LayoutRoot.Resources["DefaultMarkerSymbol"] as ESRI.ArcGIS.Client.Symbols.Symbol;
                graphicsLayer.Graphics.Add(selectedFeature);

                MapPoint p = selectedFeature.Geometry as MapPoint;

                Map.ZoomTo(new Envelope(p.X - 10, p.Y - 10, p.X + 10, p.Y + 10));
0 Kudos