// Calculates the linear distance between two user-defined geometries private async void DistanceButton_Click(object sender, RoutedEventArgs e) { try { txtResults.Visibility = Visibility.Collapsed; _graphicsLayer.Graphics.Clear(); // wait for user to draw a polyline var line = await mapView.Editor.RequestShapeAsync(DrawShape.Polyline, _lineSymbol); _graphicsLayer.Graphics.Add(new Graphic(line, _lineSymbol)); // wait for user to draw a point var point = await mapView.Editor.RequestPointAsync(); _graphicsLayer.Graphics.Add(new Graphic(point, _pointSymbol)); // Calc distance between between line and point double distance = GeometryEngine.DistanceFromGeometry(line, point) * METERS_TO_MILES; txtResults.Text = string.Format("Distance between geometries: {0:0.000} miles", distance); txtResults.Visibility = Visibility.Visible; } catch (Exception ex) { var _ = new MessageDialog("Distance Calculation Error: " + ex.Message, "Sample Error").ShowAsync(); txtResults.Visibility = Visibility.Collapsed; _graphicsLayer.Graphics.Clear(); } }
// calculates how far down a line a certain point on the line is located as a value from 0..1 private double GetFractionAlongLine(Polyline segment, ProximityResult proximity, MapPoint location) { double distance1 = 0; double distance2 = 0; int pointIndex = proximity.PointIndex; int vertexCount = segment.Paths[0].Count; var vertexPoint = segment.Paths[proximity.PartIndex][pointIndex]; Coordinate previousPoint; int onSegmentIndex = 0; //Detect which line segment we currently are on if (pointIndex == 0) //Snapped to first vertex { onSegmentIndex = 0; } else if (pointIndex == vertexCount - 1) //Snapped to last vertex { onSegmentIndex = segment.Paths[0].Count - 2; } else { Coordinate nextPoint = segment.Paths[0][pointIndex + 1]; var d1 = GeometryEngine.DistanceFromGeometry(new MapPoint(vertexPoint, segment.SpatialReference), new MapPoint(nextPoint, segment.SpatialReference)); var d2 = GeometryEngine.DistanceFromGeometry(location, new MapPoint(nextPoint, segment.SpatialReference)); if (d1 < d2) { onSegmentIndex = pointIndex - 1; } else { onSegmentIndex = pointIndex; } } previousPoint = segment.Paths[0][0]; for (int j = 1; j < onSegmentIndex + 1; j++) { Coordinate point = segment.Paths[0][j]; distance1 += GeometryEngine.DistanceFromGeometry(new MapPoint(previousPoint, segment.SpatialReference), new MapPoint(point, segment.SpatialReference)); previousPoint = point; } distance1 += GeometryEngine.DistanceFromGeometry(new MapPoint(previousPoint, segment.SpatialReference), location); previousPoint = segment.Paths[0][onSegmentIndex + 1]; distance2 = GeometryEngine.DistanceFromGeometry(location, new MapPoint(previousPoint, segment.SpatialReference)); previousPoint = vertexPoint; for (int j = onSegmentIndex + 2; j < segment.Paths[0].Count; j++) { Coordinate point = segment.Paths[0][j]; distance2 += GeometryEngine.DistanceFromGeometry(new MapPoint(previousPoint, segment.SpatialReference), new MapPoint(point, segment.SpatialReference)); previousPoint = point; } //var previousPoint = proximity.PointIndex ? segment.GetPoint(proximity.PartIndex + 1, 0) : segment.GetPoint(proximity.PartIndex, proximity.PointIndex + 1); if (distance1 + distance2 == 0) { return(1); } return(distance1 / (distance1 + distance2)); }