/// <summary> /// Updates the measurements and message shown on the popup based on the in-progress /// geometry, including the passed-in point /// </summary> /// <param name="mapPoint"></param> private void updateMeasurements(MapPoint mapPoint) { if (_currentDrawVertices != null) { double length = 0d; double area = 0d; string measurement1 = null; string measurement2 = null; switch (AssociatedObject.DrawMode) { case DrawMode.Freehand: // Copy the set of vertices and add the passed-in point to it PointCollection tmpPts = _currentDrawVertices.Clone(); tmpPts.Add(mapPoint.Clone()); // Check whether a polyline or polygon is being drawn FreehandDrawMode freehandeMode = AttachedProperties.GetFreehandDrawMode(AssociatedObject); if (freehandeMode == FreehandDrawMode.Polyline) { // Get the length of the line and update the measurement text Polyline line = new Polyline() { SpatialReference = _currentDrawVertices[0].SpatialReference }; line.Paths.Add(tmpPts); length = line.Length(); measurement1 = string.Format(Strings.LengthLabelFormat, getLengthString(length)); } else if (freehandeMode == FreehandDrawMode.Polygon) { // Close the polygon by duplicating the first vertex and adding it to the end tmpPts.Add(tmpPts[0].Clone()); Polygon polygon = new Polygon { SpatialReference = _currentDrawVertices[0].SpatialReference }; polygon.Rings.Add(tmpPts); // Get the perimeter of the polygon and update the measurement text length = polygon.Perimeter(); measurement1 = string.Format(Strings.PerimeterLabelFormat, getLengthString(length)); } break; case DrawMode.Polyline: // Create a line containing the segement of the polyline currently being drawn. // First, get the most recently drawn point tmpPts = new PointCollection(); tmpPts.Add(_currentDrawVertices[_currentDrawVertices.Count - 1].Clone()); // Add the passed-in point tmpPts.Add(mapPoint.Clone()); // Create a polyline with the points Polyline polyline = new Polyline() { SpatialReference = _currentDrawVertices[0].SpatialReference }; polyline.Paths.Add(tmpPts); // Calculate the length and update the segment measurement text length = polyline.Length(); measurement1 = string.Format(Strings.SegmentLengthLabelFormat, getLengthString(length)); // If there already two or more points in the polyline, calculate total length as well if (_currentDrawVertices.Count > 1) { // Copy the current set of vertices and add the passed-in point tmpPts = _currentDrawVertices.Clone(); tmpPts.Add(mapPoint.Clone()); // Create a polyline with the points polyline = new Polyline() { SpatialReference = _currentDrawVertices[0].SpatialReference }; polyline.Paths.Add(tmpPts); // Get the length length = polyline.Length(); } // Update measurement text showing the total length of the line measurement2 = string.Format(Strings.TotalLengthLabelFormat, getLengthString(length)); break; case DrawMode.Rectangle: // Create an envelope from the initial point and the passed-in piont Envelope env = new Envelope(_currentDrawVertices[0], mapPoint) { SpatialReference = mapPoint.SpatialReference }; // Get the perimeter and area of the envelope length = env.Perimeter(); area = env.Area(); // Update the measurement text measurement1 = string.Format(Strings.PerimeterLabelFormat, getLengthString(length)); measurement2 = string.Format(Strings.AreaLabelFormat, getAreaString(area)); break; case DrawMode.Circle: case DrawMode.Ellipse: // Get the initial (center) point and the current point tmpPts = new PointCollection(); tmpPts.Add(_currentDrawVertices[0].Clone()); //tmpPts.Add(_currentDrawVertices[_currentDrawVertices.Count - 1].Clone()); tmpPts.Add(mapPoint.Clone()); // Create a line with the points polyline = new Polyline() { SpatialReference = _currentDrawVertices[0].SpatialReference }; polyline.Paths.Add(tmpPts); // Calculate the length of the line. This is the radius. length = polyline.Length(); // For circles, this info is sufficient to calculate perimeter anda area if (AssociatedObject.DrawMode == DrawMode.Circle) { // Calculate perimeter (circumference) and area double circumference = 2 * length * Math.PI; area = Math.PI * Math.Pow(length, 2); // Display measures measurement1 = string.Format(Strings.PerimeterLabelFormat, getLengthString(circumference)); measurement2 = string.Format(Strings.AreaLabelFormat, getAreaString(area)); } else // Ellipse { // insufficient info for perimeter & area, so just display radius measurement1 = string.Format(Strings.RadiusLabelFormat, getLengthString(length)); } break; case DrawMode.Polygon: // Can only calculate perimeter and area if there are already two or more // vertices in the polygon if (_currentDrawVertices.Count > 1) { // Copy the existing vertices and add the passed-in point tmpPts = _currentDrawVertices.Clone(); tmpPts.Add(mapPoint.Clone()); // Create a polygon from the points Polygon p = new Polygon() { SpatialReference = mapPoint.SpatialReference }; p.Rings.Add(tmpPts); // Calculate the perimeter as well as the area length = p.Perimeter(); area = p.Area(); // Update measurement text measurement1 = string.Format(Strings.PerimeterLabelFormat, getLengthString(length)); measurement2 = string.Format(Strings.AreaLabelFormat, getAreaString(area)); } break; default: break; } // Update the draw popup with the measurement text if (measurement1 != null) { PopupText = _drawInstructions + "\n" + measurement1; } if (measurement2 != null) { PopupText += "\n" + measurement2; } } }
/// <summary> /// Sets the freehand drawing mode of the Draw object /// </summary> public static void SetFreehandDrawMode(Draw draw, FreehandDrawMode mode) { draw.SetValue(FreehandDrawModeProperty, mode); }