// 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);
        }
示例#2
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";
        }
示例#3
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);
        }
        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";
        }
示例#5
0
        /// <summary>
        /// The simulator moves to the closest coordinate on the route.
        /// </summary>
        public void SeekClosestCoordinate()
        {
            if (State != SimulationState.Seeking)
            {
                var nearestResult = GeometryEngine.NearestCoordinate(_route.RouteGeometry, _location);
                var nearest       = nearestResult.Coordinate;

                _target = nearest;
                State   = SimulationState.Seeking;
            }
        }
示例#6
0
        /// <summary>
        /// The simulator moves to the start of the route.
        /// </summary>
        public void SeekStart()
        {
            // Snap to the closest point on route 0
            if (State != SimulationState.Seeking)
            {
                var route0 = GeometryHelpers.Flatten(_route.RouteGeometry);
                var result = GeometryEngine.NearestCoordinate(route0, _location);

                _target = result.Coordinate;
                State   = SimulationState.Seeking;
            }
        }
        private async void StartButton_Click(object sender, RoutedEventArgs e)
        {
            inputGraphicsLayer.Graphics.Clear();

            //hide ui elements
            InstructionsTextBlock.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
            StartButton.Visibility           = Windows.UI.Xaml.Visibility.Collapsed;
            ResultsTextBlock.Visibility      = Visibility.Collapsed;

            //show ui elements
            PolylineInstructionsTextBlock.Visibility = Windows.UI.Xaml.Visibility.Visible;
            PointInstructionsTextBlock.Visibility    = Windows.UI.Xaml.Visibility.Collapsed;


            //Get the user's input geometry and add it to the map
            inputPolylineGeometry = (await mapView1.Editor.RequestShapeAsync(DrawShape.Polyline)) as Polyline;
            inputGraphicsLayer.Graphics.Add(new Graphic {
                Geometry = inputPolylineGeometry
            });

            PolylineInstructionsTextBlock.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
            PointInstructionsTextBlock.Visibility    = Windows.UI.Xaml.Visibility.Visible;


            inputPointGeometry = (await mapView1.Editor.RequestShapeAsync(DrawShape.Point)) as MapPoint;
            inputGraphicsLayer.Graphics.Add(new Graphic {
                Geometry = inputPointGeometry, Symbol = new SimpleMarkerSymbol {
                    Color = Colors.Red, Size = 12
                }
            });

            //Convert to WebMercator so that we can get the results in meters
            var wmPolyline = GeometryEngine.Project(inputPolylineGeometry, SpatialReferences.WebMercator);
            var wmPoint    = GeometryEngine.Project(inputPointGeometry, SpatialReferences.WebMercator) as MapPoint;

            var result = GeometryEngine.NearestCoordinate(wmPolyline, wmPoint);



            var resultStr = string.Format("Distance : {0} meters", result.Distance.ToString("n3"));

            ResultsTextBlock.Text                    = resultStr;
            ResultsTextBlock.Visibility              = Visibility.Visible;
            StartButton.Visibility                   = Windows.UI.Xaml.Visibility.Visible;
            ResetButton.Visibility                   = Windows.UI.Xaml.Visibility.Collapsed;
            PointInstructionsTextBlock.Visibility    = Windows.UI.Xaml.Visibility.Collapsed;
            PolylineInstructionsTextBlock.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
        }
示例#8
0
文件: GeoUtil.cs 项目: mrc-glk/gsec
        public static MapPoint GetNearestCoordinateInGraphicsCollection(MapPoint point, IList <Graphic> graphics)
        {
            ProximityResult nearest = null;
            MapPoint        loc     = point.ToWgs84(); // :)

            foreach (var graphic in graphics)
            {
                ProximityResult result = GeometryEngine.NearestCoordinate(graphic.Geometry, loc);
                if (nearest == null || result.Distance < nearest.Distance)
                {
                    nearest = result;
                }
            }

            return(nearest?.Coordinate);
        }
示例#9
0
        private void UpdateWander(double speed)
        {
            if (_location == null)
            {
                return;
            }

            // Find closest point on the route
            var nearestResult = GeometryEngine.NearestCoordinate(_route.RouteGeometry, _location);
            var nearest       = nearestResult.Coordinate;

            // Move away from the nearest coordinate
            var bearingToward = GeometryHelpers.BearingGeodetic(_location, nearest);
            var bearing       = GeometryHelpers.NormalizeBearing(bearingToward + 180); // directly away

            // Keep on moving
            _course   = bearing;
            _location = GeometryHelpers.CreatePointAlongGeodetic(_location, bearing, speed);
        }
        /// <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);
                }
            }
        }