Select to view content in your preferred language

zoom to points

901
7
05-21-2012 02:11 AM
YassineEL_JADIDI
Deactivated User
hello,
i created spatial query for points. the selected points informations appear in a datagrid. http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#SpatialQuery
i want to zoom into point selected in the datagrid. i wish i explained well my problem.
any idea please ?
0 Kudos
7 Replies
wangzhifang
Frequent Contributor
Just try this:
MyMap.ZoomToResolution(MyMap.Resolution / MyMap.ZoomFactor, mapPoint);
0 Kudos
YassineEL_JADIDI
Deactivated User
thank you for your reply.
but i want to know what is "mapPoint" .
because it dont work in my code, can you use it in an example to understand his role please !
0 Kudos
JoeHershman
MVP Regular Contributor
In the code example from the sample you pointed to the SelectionChanged Event returns a Graphic collection (IList) as shown below e.AddedItems.


        private void QueryDetailsDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            foreach (Graphic g in e.AddedItems)
                g.Select();

            foreach (Graphic g in e.RemovedItems)
                g.UnSelect();
        }




The Graphic object has a Geometry property and your case because you selected points this Geomtry would be a MapPoint object. 
Something like this below should get the MapPoint you need


        private void QueryDetailsDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            //This would work with multiple points being selected
            ESRI.ArcGIS.Client.Geometry.PointCollection points = new ESRI.ArcGIS.Client.Geometry.PointCollection();

            foreach ( Graphic g in e.AddedItems )
            {
                MapPoint mapPoint = g.Geometry as MapPoint;
                if (mapPoint != null )
                {
                    points.Add(mapPoint);
                }
            }

            MultiPoint multiPoint = new MultiPoint(points);
            MyMap.ZoomTo(multiPoint.Extent);

            //If you user was only allowed to select a single point it would be something like this

             Graphic g = e.AddedItems[0] as Graphic;
             MapPoint mapPoint = g.Geometry as MapPoint;
             if (mapPoint != null )
             {
                   MyMap.ZoomToResolution(MyMap.Resolution / MyMap.ZoomFactor, mapPoint);  //As shown Above
              }



            foreach (Graphic g in e.RemovedItems)
                g.UnSelect();
        }



Hope that helps
-Joe
Thanks,
-Joe
0 Kudos
YassineEL_JADIDI
Deactivated User
thank you for your reply,
i tired the code but it give me that "g" is already declared..i changed it by another variable, it work but when i click in the item point in datagrid....it dont zoom to point..it zoom on map after any click in items of datagrid.
0 Kudos
JoeHershman
MVP Regular Contributor
Yassine,

Sorry about the variable name error, the sample was not actually meant to be used as is, it was intended to show the two possible ways to setup the code depending on if a user was allowed to select only one row, or select more than one row

I do not think I am clear about what you are trying to zoom to.

What I thought you wanted to do is select a number of points in your map using a spatial query.  For example, you would draw a box around a number of points and those points would then show up in the selected points datagrid.  Then when you select an item in the data grid it would zoom to that point.  So every time you click an item in the grid it zooms to a location on the map.

For the sample I changed the code to this:

        private void QueryDetailsDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            foreach (Graphic g in e.AddedItems)
            {
                g.Select();

                MyMap.ZoomTo(g.Geometry);
            }

            foreach (Graphic g in e.RemovedItems)
                g.UnSelect();
        }


Which will now zoom to the state that was selected when the user clicks on the item in the datagrid.

Can you please try to clarify if this is what you are trying to do.  I think that your goal is different from my understanding.

Thanks
-Joe
Thanks,
-Joe
0 Kudos
YassineEL_JADIDI
Deactivated User
Thanks joe, yes understood what i want exactly
you would draw a box around a number of points and those points would then show up in the selected points datagrid. Then when you select an item in the data grid it would zoom to that point. So every time you click an item in the grid it zooms to a location on the map.

i tried the last code you gave but it dont work....it do not do anything.
0 Kudos
JenniferNery
Esri Regular Contributor
0 Kudos