Select to view content in your preferred language

Need help in drawing a circle in code behind.

734
3
09-05-2011 06:44 AM
SanajyJadhav
Deactivated User
Hi,

I am drawing a circle in a code behind using following code.:

code:

[PHP]
  private ESRI.ArcGIS.Client.Geometry.Polygon createCirclePolygon(MapPoint ptCenter,
            double dRadius, int n, ESRI.ArcGIS.Client.Map map)
        {
            //create new point collection
            ESRI.ArcGIS.Client.Geometry.PointCollection pc = new ESRI.ArcGIS.Client.Geometry.PointCollection();

            //center point in pixels
            System.Windows.Point ptCenterScreen = map.MapToScreen(ptCenter);

            n = n <= 0 ? 32 : n;
            for (int i = 0; i < n; i++)
            {
                //ecah point on the circle, counterclockwise
                double angle = (Math.PI * 2) / n * i;
                System.Windows.Point p = new System.Windows.Point()
                {
                    X = ptCenterScreen.X + dRadius * Math.Cos(angle),
                    Y = ptCenterScreen.Y + dRadius * Math.Sin(angle)
                };
                pc.Add(map.ScreenToMap(p));
            };
            //add the last, which is exact the first, point.
            pc.Add(map.ScreenToMap(new System.Windows.Point()
            {
                X = ptCenterScreen.X + dRadius,
                Y = ptCenterScreen.Y
            }));


            ESRI.ArcGIS.Client.Geometry.Polygon polyg = new ESRI.ArcGIS.Client.Geometry.Polygon();
            polyg.SpatialReference = map.SpatialReference;

            polyg.Rings.Add(pc);

            return polyg;
        }
[/PHP]

I am seeing an issue in above code.If I draw a circle, it is drawn. Now, if I zoom out and again draw a circle with same radius, it should be drawn a little smaller than previous one. But it is getting drawn bigger.So, when map extent is changed, size of the circle (though radius is same) is getting changed.

I think there is some scrren coordinates issue and I am not being able to spot it. Can anybody help me on this issue? I would appreciate any help.

Regards,
Sanjay.
0 Kudos
3 Replies
wangzhifang
Frequent Contributor
Actually, you could draw a circle by directly using the Draw class in Silverlight API.
If you choose to draw it your self, and if you want the circle to change with the zoom in/out of the Map, you should draw it in map coordinate system; if you want the circle to remain the same size, no matter if the map's zoom in/out, you should draw it in screen coordinate and add it to an ElementLayer. Ref: http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#ElementLayer
0 Kudos
SanajyJadhav
Deactivated User
Thanks for getting back to me Wang.

I would give a try.

-
Sanajy.
0 Kudos
SanajyJadhav
Deactivated User
Wang,

It worked.I drew my circle in map coordinate system and now it seems fine.

Thanks for the suggestion.
cheers,

Sanjay.
0 Kudos