// Calculate the route private async void MyMapView_MapViewDoubleTapped(object sender, Esri.ArcGISRuntime.Controls.MapViewInputEventArgs e) { if (!_isMapReady || _stopsOverlay.Graphics.Count() < 2) { return; } try { e.Handled = true; panelResults.Visibility = Visibility.Collapsed; progress.Visibility = Visibility.Visible; if (_routeTask == null) { _routeTask = await Task.Run <LocalRouteTask>(() => new LocalRouteTask(_localRoutingDatabase, _networkName)); } RouteParameters routeParams = await _routeTask.GetDefaultParametersAsync(); routeParams.OutSpatialReference = MyMapView.SpatialReference; routeParams.ReturnDirections = true; routeParams.DirectionsLengthUnit = LinearUnits.Miles; routeParams.DirectionsLanguage = new CultureInfo("en-US"); routeParams.SetStops(_stopsOverlay.Graphics); var routeResult = await _routeTask.SolveAsync(routeParams); if (routeResult == null || routeResult.Routes == null || routeResult.Routes.Count() == 0) { throw new ApplicationException("No route could be calculated"); } var route = routeResult.Routes.First(); _routesOverlay.Graphics.Add(new Graphic(route.RouteFeature.Geometry)); _directionsOverlay.GraphicsSource = route.RouteDirections.Select(rd => GraphicFromRouteDirection(rd)); var totalTime = route.RouteDirections.Select(rd => rd.Time).Aggregate(TimeSpan.Zero, (p, v) => p.Add(v)); var totalLength = route.RouteDirections.Select(rd => rd.GetLength(LinearUnits.Miles)).Sum(); txtRouteTotals.Text = string.Format("Time: {0:h':'mm':'ss} / Length: {1:0.00} mi", totalTime, totalLength); await MyMapView.SetViewAsync(route.RouteFeature.Geometry.Extent.Expand(1.2)); } catch (AggregateException ex) { var innermostExceptions = ex.Flatten().InnerExceptions; if (innermostExceptions != null && innermostExceptions.Count > 0) { MessageBox.Show(innermostExceptions[0].Message, "Sample Error"); } else { MessageBox.Show(ex.Message, "Sample Error"); } } catch (System.Exception ex) { MessageBox.Show(ex.Message, "Sample Error"); } finally { progress.Visibility = Visibility.Collapsed; if (_directionsOverlay.Graphics.Count() > 0) { panelResults.Visibility = Visibility.Visible; } } }
public async void AddStops() { // If the user clicked the SolveRouteButton more than once, clear out any existing stops and routes graphics. routeGraphicsLayer.Graphics.Clear(); stopsGraphicsLayer.Graphics.Clear(); try { // Mouse click 1: setting the start point for the route // --------------------------------------------------- // Get the Editor from the MapView. Editor startPointEditor = this.mapView.Editor; // Get the MapPoint from where the user clicks in the Map Control. This will be the starting point for the route. MapPoint startLocationMapPoint = await startPointEditor.RequestPointAsync(); // Create a new Graphic and set it's geometry to the user clicked MapPoint Graphic startPointGraphic = new Graphic(); startPointGraphic.Geometry = startLocationMapPoint; // Add the start point graphic to the stops GraphicsLayer. stopsGraphicsLayer.Graphics.Add(startPointGraphic); // Mouse click 2: setting the end point for the route // --------------------------------------------------- // Get the Editor from the MapView. Editor endPointEditor = this.mapView.Editor; // Get the MapPoint from where the user clicks in the Map Control. This will be the ending point for the route. MapPoint endLocationMapPoint = await startPointEditor.RequestPointAsync(); // Create a new Graphic and set it's geometry to the user clicked MapPoint Graphic endPointGraphic = new Graphic(); endPointGraphic.Geometry = endLocationMapPoint; // Add the start point graphic to the stops GraphicsLayer. stopsGraphicsLayer.Graphics.Add(endPointGraphic); // Set the arguments for the RouteTask: // Get the RouteParameters from the RouteTask. RouteParameters routeParameters = await routeTask.GetDefaultParametersAsync(); // Define the settings for the RouteParameters. This includes setting the SpatialReference, // ReturnDirections, DirectionsLengthUnit and Stops. routeParameters.OutSpatialReference = this.mapView.SpatialReference; routeParameters.ReturnDirections = false; routeParameters.DirectionsLengthUnit = LinearUnits.Kilometers; // Define a List of Graphics based upon the user start and end clicks in the Map Control that will serve as input // parameters for the RouteTask operation. List <Graphic> graphicsStops = new List <Graphic>(); graphicsStops.Add(new Graphic(startLocationMapPoint)); graphicsStops.Add(new Graphic(endLocationMapPoint)); // Set the stops for the Route. routeParameters.SetStops(graphicsStops); // Call the asynchronous function to solve the RouteTask. RouteResult routeResult = await routeTask.SolveAsync(routeParameters); // Ensure we got at least one route back. if (routeResult.Routes.Count > 0) { // Get the first Route from the List of Routes Route firstRoute = routeResult.Routes[0]; // Get the Geometry from the Graphic in the first Route. Geometry routeGeometry = firstRoute.RouteFeature.Geometry; // Create a new Graphic based upon the Graphic. Graphic routeGraphic = new Graphic(routeGeometry); // Add the Graphic (a polyline) to the route GraphicsLayer. this.routeGraphicsLayer.Graphics.Add(routeGraphic); } } catch (System.AggregateException ex) { // There was a problem, display the results to the user. var innermostExceptions = ex.Flatten().InnerExceptions; if (innermostExceptions != null && innermostExceptions.Count > 0) { Messenger.Default.Send <NotificationMessage>(new NotificationMessage((innermostExceptions[0].Message.ToString()))); } else { Messenger.Default.Send <NotificationMessage>(new NotificationMessage(ex.Message.ToString())); } } catch (System.Exception ex) { // There was a problem, display the results to the user. Messenger.Default.Send <NotificationMessage>(new NotificationMessage("Error: " + ex.Message.ToString())); } finally { } }