Select to view content in your preferred language

Measure Tool Sample code

2635
6
09-02-2010 10:13 AM
PLadd
by
Frequent Contributor
Would anyone be kind enough to provide a complete sample code for using the measure tool in a State Plane coordinate system?  My understanding is that I need to project the graphic into my coordinate system in order for the distance to be accurate.

I have code behind for using the measure tool (it works but the values are wrong):

private void btnMeasureLength_ft_Click(object sender, RoutedEventArgs e)
        {
            MyDrawSurface.DrawMode = DrawMode.None;
            myMeasureAction m = new myMeasureAction();
            m.MeasureMode = myMeasureAction.Mode.Polyline;
            m.DisplayTotals = true;
            m.LineSymbol = DefaultLineSymbol;
            m.DistanceUnit = ESRI.ArcGIS.Client.Actions.DistanceUnit.Feet;
            m.Attach(MyMap);
            m.Execute();
        }


I also have a geometry service running.

What I don't get is where to put the following code.  I also don't understand how to get "graphic" below.

GeometryService geometryService = new GeometryService(url);
geometryService.ProjectCompleted += GeometryService_ProjectCompleted;
geometryService.Failed += GeometryService_Failed;

List<Graphic> graphicList = new List<Graphic>();
graphicList.Add(graphic);

geometryService.ProjectAsync(graphicList, new SpatialReference(2234));

Thanks for those who respond . . . . .
0 Kudos
6 Replies
dotMorten_esri
Esri Notable Contributor
If you want to measure in State Plane units together with the MeasureAction, your map must be using a state plane projection.

What class is "myMeasureAction" ???
0 Kudos
AaronAndrus
Emerging Contributor
Does the map really have to be in state plane? Can't you just reproject it using the geometry service like the op is trying?

This might be of help:

http://blogs.esri.com/Dev/blogs/arcgisserver/archive/2010/03/05/Measuring-distances-and-areas-when-y...
0 Kudos
AaronAndrus
Emerging Contributor
Here's an example of re-projecting using the geometryservice. The same concept applies with measurements. In this case I am projecting lat,long coordinates to the spatial reference of my map.
        private void ReprojectPoints()
        {
            GraphicsLayer glayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;
            
            GeometryService geos = new GeometryService("http://myserver/ArcGIS/rest/services/Geometry/GeometryServer");
            geos.ProjectCompleted += new EventHandler<GraphicsEventArgs>(geos_ProjectCompleted);
            geos.Failed += new EventHandler<TaskFailedEventArgs>(geos_Failed);

            geos.ProjectAsync(glayer.Graphics.ToList(), MyMap.SpatialReference);
        }

        void geos_Failed(object sender, TaskFailedEventArgs e)
        {
            MessageBox.Show(e.Error.Message);
        }

        void geos_ProjectCompleted(object sender, GraphicsEventArgs e)
        {
            GraphicsLayer glayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;
            glayer.ClearGraphics();

            foreach (Graphic g in e.Results)
            {
                g.Symbol = SimpleMarker;
                glayer.Graphics.Add(g);
            }
        }
0 Kudos
PLadd
by
Frequent Contributor
Morten,

My map is in State Plane but the measurements for length and area are way off (curiously radius works fine).  My understanding is that the measure tool only works with geographic coordinates and we have to project the graphic to state plan for the measure tool to work in state plane.

public class myMeasureAction : ESRI.ArcGIS.Client.Actions.MeasureAction

Aaron,

Thanks for the link - I couldn't figure out how to implement that.  With your sample code, I'm not sure where to implement that either.  I guess I'm not understanding the measure tool and having to reproject the graphic.  My map is in state plane coordinates.  When I click on the measure tool I then start measuring on the map and it's giving me distances as I move the mouse. 

But at what point am I supposed to reproject a graphic that I am still drawing and which is measuring distances inaccurately?
0 Kudos
PLadd
by
Frequent Contributor
Is there really any science in Computer Science?  Here's what I had to do to make all my measure tools work.  The measurement distances were way off and at first I thought I had to reproject the graphics used in the measurement to a geographic coordinate system (I'm using a projected coordinate system - state plane).  But it didn't make sense that the radius tool worked and not the others.  They were all in code-behind (I can't remember why I did that).  So I put them back onto the xaml page and they all started working again - except for the radius tool.  So I put the radius tool in code-behind and the others stayed in the xaml page and I am happy to report all the measure tools are now working.  And that is why there is no science in Computer Science!
0 Kudos
WilliamDonahue
Emerging Contributor
If you are using the measure tool like the one they display in the sample code it is quite simple... just set the map units to feet and it is in state plane.
0 Kudos