Beispiel #1
0
        private void MyMapView_GeoViewTapped(object sender, GeoViewInputEventArgs geoViewInputEventArgs)
        {
            // Get the tapped location.
            MapPoint tappedLocation = geoViewInputEventArgs.Location;

            // Show the tapped location.
            _tappedLocationGraphic.Geometry = tappedLocation;

            // Get the nearest vertex in the polygon.
            ProximityResult nearestVertexResult = GeometryEngine.NearestVertex(_polygonGraphic.Geometry, tappedLocation);

            // Get the nearest coordinate in the polygon.
            ProximityResult nearestCoordinateResult =
                GeometryEngine.NearestCoordinate(_polygonGraphic.Geometry, tappedLocation);

            // Get the distance to the nearest vertex in the polygon.
            int distanceVertex = (int)(nearestVertexResult.Distance / 1000);

            // Get the distance to the nearest coordinate in the polygon.
            int distanceCoordinate = (int)(nearestCoordinateResult.Distance / 1000);

            // Show the nearest vertex in blue.
            _nearestVertexGraphic.Geometry = nearestVertexResult.Coordinate;

            // Show the nearest coordinate in red.
            _nearestCoordinateGraphic.Geometry = nearestCoordinateResult.Coordinate;

            // Show the distances in the UI.
            _distanceLabel.Text = $"Vertex dist: {distanceVertex} km, Point dist: {distanceCoordinate} km";
        }
        // Accept user click point and find nearest target geometry point
        private async Task GetNearestCoordAsync(bool vertexOnly)
        {
            var target = _targetOverlay.Graphics.Select(g => g.Geometry).FirstOrDefault();

            if (target == null)
            {
                return;
            }

            txtInstruct.Text = "Click the map to find the nearest coordinate";
            var point = await MyMapView.Editor.RequestPointAsync();

            ProximityResult result = null;

            if (vertexOnly)
            {
                result = GeometryEngine.NearestVertex(target, point);
            }
            else
            {
                result = GeometryEngine.NearestCoordinate(target, point);
            }

            _coordinateOverlay.Graphics.Clear();
            _coordinateOverlay.Graphics.Add(new Graphic(point, _userPointSymbol));
            _coordinateOverlay.Graphics.Add(new Graphic(result.Point));

            txtResult.Visibility = Visibility.Visible;
            txtResult.Text       = string.Format("Nearest Point: Index: {0}, Distance: {1:0.000}", result.PointIndex, result.Distance);
        }
Beispiel #3
0
        private async Task UpdateGeometry(MapPoint point)
        {
            if (_selectedFeature.Geometry is Polyline line)
            {
                // Get the nearest point on the selected line.
                ProximityResult nearestVertex = GeometryEngine.NearestVertex(line, point);

                // Create a new polyline.
                PolylineBuilder polylineBuilder = new PolylineBuilder(line);
                Part            part            = polylineBuilder.Parts[nearestVertex.PartIndex];

                // Replace the nearest point with the new point.
                part.SetPoint(nearestVertex.PointIndex, point);

                // Update the geometry of the feature.
                _selectedFeature.Geometry = GeometryEngine.Project(polylineBuilder.ToGeometry(), _selectedFeature.Geometry.SpatialReference);
                await _selectedFeature.FeatureTable.UpdateFeatureAsync(_selectedFeature);
            }
            else if (_selectedFeature.Geometry is MapPoint)
            {
                // Update the geometry of the feature.
                _selectedFeature.Geometry = point;
                await _selectedFeature.FeatureTable.UpdateFeatureAsync(_selectedFeature);
            }

            // Clear the selection.
            (_selectedFeature.FeatureTable.Layer as FeatureLayer).ClearSelection();
            _selectedFeature = null;
        }
        private void MapViewTapped(object sender, Esri.ArcGISRuntime.Xamarin.Forms.GeoViewInputEventArgs geoViewInputEventArgs)
        {
            // Get the tapped location
            MapPoint tappedLocation = geoViewInputEventArgs.Location;

            // Show the tapped location
            _tappedLocationGraphic.Geometry = tappedLocation;

            // Get the nearest vertex in the polygon
            ProximityResult nearestVertexResult = GeometryEngine.NearestVertex(_polygonGraphic.Geometry, tappedLocation);

            // Get the nearest coordinate in the polygon
            ProximityResult nearestCoordinateResult =
                GeometryEngine.NearestCoordinate(_polygonGraphic.Geometry, tappedLocation);

            // Get the distance to the nearest vertex in the polygon
            int distanceVertex = (int)(nearestVertexResult.Distance / 1000);

            // Get the distance to the nearest coordinate in the polygon
            int distanceCoordinate = (int)(nearestCoordinateResult.Distance / 1000);

            // Show the nearest vertex in blue
            _nearestVertexGraphic.Geometry = nearestVertexResult.Coordinate;

            // Show the nearest coordinate in red
            _nearestCoordinateGraphic.Geometry = nearestCoordinateResult.Coordinate;

            // Show the distances in the UI
            ResultsLabel.Text = $"Vertex dist: {distanceVertex} km, Point dist: {distanceCoordinate} km";
        }
Beispiel #5
0
        private void MapViewTapped(object sender, GeoViewInputEventArgs geoViewInputEventArgs)
        {
            // Get the tapped location
            MapPoint tappedLocation = (MapPoint)GeometryEngine.NormalizeCentralMeridian(geoViewInputEventArgs.Location);

            // Show the tapped location
            _tappedLocationGraphic.Geometry = tappedLocation;

            // Get the nearest vertex in the polygon
            ProximityResult nearestVertexResult = GeometryEngine.NearestVertex(_polygonGraphic.Geometry, tappedLocation);

            // Get the nearest coordinate in the polygon
            ProximityResult nearestCoordinateResult =
                GeometryEngine.NearestCoordinate(_polygonGraphic.Geometry, tappedLocation);

            // Get the distance to the nearest vertex in the polygon
            int distanceVertex = (int)(nearestVertexResult.Distance / 1000);

            // Get the distance to the nearest coordinate in the polygon
            int distanceCoordinate = (int)(nearestCoordinateResult.Distance / 1000);

            // Show the nearest vertex in blue
            _nearestVertexGraphic.Geometry = nearestVertexResult.Coordinate;

            // Show the nearest coordinate in red
            _nearestCoordinateGraphic.Geometry = nearestCoordinateResult.Coordinate;

            // Show the distances in the UI
            ResultsLabel.Content =
                string.Format("Vertex dist: {0} km, Point dist: {1} km", distanceVertex, distanceCoordinate);
        }
        /// <summary>
        /// Call this to set your current location and update directions based on that.
        /// </summary>
        /// <param name="location"></param>
        public void SetCurrentLocation(MapPoint location)
        {
            List <string> propertyNames = new List <string>(new string[] {
                "NextManeuver", "WaypointLocation", "SnappedLocation", "CurrentDirection", "TimeToWaypoint",
                "DistanceToDestination", "DistanceToWaypoint", "TimeToDestination",
                "MilesToDestination", "MilesToWaypoint",
            });
            RouteDirection closest         = null;
            double         distance        = double.NaN;
            MapPoint       snappedLocation = null;
            Route          direction       = null;

            // Find the route part that we are currently on by snapping to each segment and see which one is the closest
            foreach (var dir in m_route.Routes)
            {
                var closestCandidate = (from a in dir.RouteDirections
                                        where a.Geometry is Polyline
                                        select new { Direction = a, Proximity = GeometryEngine.NearestCoordinate(a.Geometry, location) }).OrderBy(b => b.Proximity.Distance).FirstOrDefault();
                if (double.IsNaN(distance) || distance < closestCandidate.Proximity.Distance)
                {
                    distance        = closestCandidate.Proximity.Distance;
                    closest         = closestCandidate.Direction;
                    snappedLocation = closestCandidate.Proximity.Point;
                    direction       = dir;
                }
            }
            if (closest != null)
            {
                var directions = direction.RouteDirections.ToList();
                var idx        = directions.IndexOf(closest);
                if (idx < directions.Count)
                {
                    RouteDirection next = directions[idx + 1];

                    //calculate how much is left of current route segment
                    var      segment           = closest.Geometry as Polyline;
                    var      proximity         = GeometryEngine.NearestVertex(segment, snappedLocation);
                    double   frac              = 1 - GetFractionAlongLine(segment, proximity, snappedLocation);
                    TimeSpan timeLeft          = new TimeSpan((long)(closest.Time.Ticks * frac));
                    double   segmentLengthLeft = (Convert.ToDouble(closest.GetLength(LinearUnits.Meters))) * frac;
                    //Sum up the time and lengths for the remaining route segments
                    TimeSpan totalTimeLeft = timeLeft;
                    double   totallength   = segmentLengthLeft;
                    for (int i = idx + 1; i < directions.Count; i++)
                    {
                        totalTimeLeft += directions[i].Time;
                        totallength   += directions[i].GetLength(LinearUnits.Meters);
                    }

                    //Update properties
                    TimeToWaypoint        = TimeSpan.FromSeconds(Math.Round(timeLeft.TotalSeconds));
                    TimeToDestination     = TimeSpan.FromSeconds(Math.Round(totalTimeLeft.TotalSeconds));
                    DistanceToWaypoint    = Math.Round(segmentLengthLeft);
                    DistanceToDestination = Math.Round(totallength);
                    SnappedLocation       = snappedLocation;
                    var maneuverType = next.ManeuverType;
                    WaypointLocation = segment.Parts.Last().LastOrDefault().EndPoint;
#if NETFX_CORE || WINDOWS_PHONE
                    var maneuverUri = new Uri(string.Format("ms-appx:///Assets/Maneuvers/{0}.png", maneuverType));
#else
                    var maneuverUri = new Uri(string.Format("pack://application:,,,/Assets/Maneuvers/{0}.png", maneuverType));
#endif
                    if (ManeuverImage != maneuverUri)
                    {
                        ManeuverImage = maneuverUri;
                        propertyNames.Add("ManeuverImage");
                    }
                    NextManeuver = next.Text;

                    RaisePropertiesChanged(propertyNames);
                }
            }
        }