Select to view content in your preferred language

Online Samples - Attribute Query - Zoom To Point

1588
7
Jump to solution
10-01-2012 01:29 PM
GreggBreton
Occasional Contributor
Has anyone customized the existing online sample "Attribute Query" to work with points instead of polygons?

Read the related thread for ZoomToPoint

Could someone provide an example of creating an envelope around a point in the context of the Attribute Query Example

Thank you
0 Kudos
1 Solution

Accepted Solutions
deleted-user-ATjHIWsdQYmT
Deactivated User
Gregg-
You may need to cast your selected feature's geometry to a MapPoint. 

(MapPoint)selectedfeature.geometrycenterAndZoom(MyMap,(MapPoint) selectedFeature.geometry,1200);

View solution in original post

0 Kudos
7 Replies
DominiqueBroux
Esri Frequent Contributor
In this thread, there is a centerAndZoom function that should do the job.
0 Kudos
GreggBreton
Occasional Contributor
Hello Dominique,

I will try to use that Center and Zoom Function within the Attribute Sample and If I get it working will post the answer

Thank you
0 Kudos
GreggBreton
Occasional Contributor
OK,

So I have this function:

 //Zoom to Point instead of Polygon for QueryTask_ExecuteCompleted
        private void centerAndZoom(ESRI.ArcGIS.Client.Map map, ESRI.ArcGIS.Client.Geometry.MapPoint mapPoint, double resolution)
        {
            double ratio = 1.0;
            if (map.Resolution != 0.0)
                ratio = resolution / map.Resolution;

            if (ratio == 1.0)
                map.PanTo(mapPoint);
            else
            {
                ESRI.ArcGIS.Client.Geometry.MapPoint mapCenter = map.Extent.GetCenter();
                double X = (mapPoint.X - ratio * mapCenter.X) / (1 - ratio);
                double Y = (mapPoint.Y - ratio * mapCenter.Y) / (1 - ratio);
                map.ZoomToResolution(resolution, new ESRI.ArcGIS.Client.Geometry.MapPoint(X, Y));
            }
        }


I comment out those lines that are for zooming to a polygon except for the envelope which defines the selectedFeatureExtent

Will have a DefaultMarkerSymbol instead of DefaultFillSymbol

 // Highlight selected feature
                selectedFeature.Symbol = LayoutRoot.Resources["DefaultMarkerSymbol"] as ESRI.ArcGIS.Client.Symbols.Symbol;
                graphicsLayer.Graphics.Add(selectedFeature);

                // Zoom to selected feature (define expand percentage)
               ESRI.ArcGIS.Client.Geometry.Envelope selectedFeatureExtent = selectedFeature.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));

                //MyMap.ZoomTo(displayExtent);
                centerAndZoom(MyMap,selectedFeature,2.0);



When I call the centerAndZoom it looks for 3 parameters correct
The map which is "MyMap"
mappoint which is "selectedFeature"
double resolution

I tried:
centerAndZoom(MyMap,selectedFeature,1200);

error : cannot convert mapPoint
0 Kudos
DominiqueBroux
Esri Frequent Contributor
centerAndZoom(MyMap,selectedFeature,1200);

error : cannot convert mapPoint

You didn't give any info about the mapPoint variable in your post.
Though I guess selectedFeature is a graphic and can't be casted to a MapPoint, should likely be:
centerAndZoom(MyMap,selectedFeature.Geometry as MapPoint,1200)
0 Kudos
GreggBreton
Occasional Contributor
You mentioned the mapPoint variable? Would that not be the selectedFeature? Just trying to understand - Apologize not sure about the variable for mapPoint?

The online example selects the feature and it is called �??selectedFeature�?�, so I am guessing I want to use the selectedFeature to zoom to and not sure about how to use the selectedFeature correctly in the centerAndZoom function that you mentioned.

So I am using the �??Attributes Query�?� states example, which zooms to a polygon. Looking to replace the following:

The original code for zooming to a Polygon
    // Zoom to selected feature (define expand percentage)
                ESRI.ArcGIS.Client.Geometry.Envelope selectedFeatureExtent = selectedFeature.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));

                MyMap.ZoomTo(displayExtent);
 

Trying to replace the above with the centerAndZoom function with the centerAndZoom function below:

private void centerAndZoom(ESRI.ArcGIS.Client.Map map, ESRI.ArcGIS.Client.Geometry.MapPoint mapPoint, double resolution)
{
double ratio = 1.0;
if (map.Resolution != 0.0)
ratio = resolution / map.Resolution;

if (ratio == 1.0)
map.PanTo(mapPoint);
else
{
ESRI.ArcGIS.Client.Geometry.MapPoint mapCenter = map.Extent.GetCenter();
double X = (mapPoint.X - ratio * mapCenter.X) / (1 - ratio);
double Y = (mapPoint.Y - ratio * mapCenter.Y) / (1 - ratio);
map.ZoomToResolution(resolution, new ESRI.ArcGIS.Client.Geometry.MapPoint(X, Y));
}
}


In the example right after the code selects the feature:

// Highlight selected feature
                selectedFeature.Symbol = LayoutRoot.Resources["DefaultMarkerSymbol"] as ESRI.ArcGIS.Client.Symbols.Symbol;
                graphicsLayer.Graphics.Add(selectedFeature);


I would like to zoom to the point utilizing the centerAndZoom Function:


Wouldn't the selectedFeature be used for the the second parameter? Is there another variable I should be using for the second parameter instead of selectedFeature?


//MyMap.ZoomTo(displayExtent);
               
centerAndZoom(MyMap,selectedFeature.Geometry???,1200);

Wouldn't accept centerAndZoom(MyMap,selectedFeature.Geometry as MapPoint,1200)



Thank you for your patience
0 Kudos
deleted-user-ATjHIWsdQYmT
Deactivated User
Gregg-
You may need to cast your selected feature's geometry to a MapPoint. 

(MapPoint)selectedfeature.geometrycenterAndZoom(MyMap,(MapPoint) selectedFeature.geometry,1200);
0 Kudos
GreggBreton
Occasional Contributor
That did the trick!

Thank you Dominique and Andrew

For all users who would like the attribute query sample customized to zoom to a point the answer is the following:

//ZoomToPoint
              centerAndZoom(MyMap, (MapPoint)selectedFeature.Geometry, 100);



So the function is centerAndZoom
//Zoom to Point instead of Polygon for QueryTask_ExecuteCompleted
        private void centerAndZoom(ESRI.ArcGIS.Client.Map map, ESRI.ArcGIS.Client.Geometry.MapPoint mapPoint, double resolution)
        {
            double ratio = 1.0;
            if (map.Resolution != 0.0)
                ratio = resolution / map.Resolution;

            if (ratio == 1.0)
                map.PanTo(mapPoint);
            else
            {
                ESRI.ArcGIS.Client.Geometry.MapPoint mapCenter = map.Extent.GetCenter();
                double X = (mapPoint.X - ratio * mapCenter.X) / (1 - ratio);
                double Y = (mapPoint.Y - ratio * mapCenter.Y) / (1 - ratio);
                map.ZoomToResolution(resolution, new ESRI.ArcGIS.Client.Geometry.MapPoint(X, Y));
            }
        }



And then in the QueryTask_ExecuteCompleted right after Highlight selected feature

// Highlight selected feature
                selectedFeature.Symbol = LayoutRoot.Resources["DefaultMarkerSymbol"] as ESRI.ArcGIS.Client.Symbols.Symbol;
                graphicsLayer.Graphics.Add(selectedFeature);


Put the following:

//ZoomToPoint
              centerAndZoom(MyMap, (MapPoint)selectedFeature.Geometry, 100);
0 Kudos