private async Task <Graphic> GetRouteGraphicAsync(MapPoint startLocation, MapPoint endLocation) { // Wait until previous routing is completed before doing another if (_isRouting) { return(null); } _isRouting = true; RouteParameters routeParameters = await _routeTask.GetDefaultParametersAsync(); routeParameters.OutSpatialReference = MapViewService.SpatialReference; routeParameters.ReturnDirections = false; routeParameters.DirectionsLengthUnit = LinearUnits.Kilometers; // Add endpoints to for the route List <Graphic> graphicsStops = new List <Graphic>(); graphicsStops.Add(new Graphic() { Geometry = startLocation }); graphicsStops.Add(new Graphic() { Geometry = endLocation }); var stops = new FeaturesAsFeature(); stops.Features = graphicsStops; stops.SpatialReference = graphicsStops[0].Geometry.SpatialReference; routeParameters.Stops = stops; try { // Since we are ignoring cancellation just give a new token everytime var routeResult = await _routeTask.SolveAsync(routeParameters, new CancellationToken()); _isRouting = false; if (routeResult.Routes.Count > 0) { return(routeResult.Routes[0].RouteGraphic); } } catch (Exception) { // At the moment exception is thrown if route task couldn't solve the route so catch } _isRouting = false; return(null); }
private async Task CalculateRouteAsync() { try { progress.Visibility = Visibility.Visible; RouteParameters routeParams = await _routeTask.GetDefaultParametersAsync(); routeParams.OutSpatialReference = MyMapView.SpatialReference; routeParams.ReturnDirections = false; routeParams.SetStops(_stopsGraphicsOverlay.Graphics); RouteResult routeResult = await _routeTask.SolveAsync(routeParams); if (routeResult.Routes.Count > 0) { _routeGraphicsOverlay.Graphics.Clear(); var route = routeResult.Routes.First().RouteFeature; _routeGraphicsOverlay.Graphics.Add(new Graphic(route.Geometry)); var meters = GeometryEngine.GeodesicLength(route.Geometry, GeodeticCurveType.Geodesic); txtDistance.Text = string.Format("{0:0.00} miles", LinearUnits.Miles.ConvertFromMeters(meters)); panelRouteInfo.Visibility = Visibility.Visible; } } catch (AggregateException ex) { var innermostExceptions = ex.Flatten().InnerExceptions; if (innermostExceptions != null && innermostExceptions.Count > 0) { MessageBox.Show(innermostExceptions[0].Message); } else { MessageBox.Show(ex.Message); } } catch (System.Exception ex) { MessageBox.Show("Error: " + ex.Message); } finally { progress.Visibility = Visibility.Collapsed; } }
private async Task SetupRouteTask() { if (!IsOnline) { _routeTask = new LocalRouteTask(_localRoutingDatabase, _networkName); } else { _routeTask = new OnlineRouteTask(new Uri(_onlineRoutingService)); } if (_routeTask != null) { _routeParams = await _routeTask.GetDefaultParametersAsync(); } }
private async Task CalculateRoute() { StartLocationText = string.Empty; EndLocationText = string.Empty; Directions = string.Empty; _routeGraphicsLayer.Graphics.Clear(); _stopsGraphicsLayer.Graphics.Clear(); try { // Mouseclick 1: MapPoint startLocation = await mapView1.Editor.RequestPointAsync(); _stopsGraphicsLayer.Graphics.Add(new Graphic() { Geometry = startLocation }); StartLocationText = " X: " + Math.Round(startLocation.X, 0).ToString() + " Y: " + Math.Round(startLocation.Y, 0).ToString(); // Mouseclick 2: MapPoint endLocation = await mapView1.Editor.RequestPointAsync(); _stopsGraphicsLayer.Graphics.Add(new Graphic() { Geometry = endLocation }); EndLocationText = " X: " + Math.Round(endLocation.X, 0).ToString() + " Y: " + Math.Round(endLocation.Y, 0).ToString(); Esri.ArcGISRuntime.Tasks.NetworkAnalyst.RouteParameters routeParams = await _routeTask.GetDefaultParametersAsync(); routeParams.OutSpatialReference = mapView1.SpatialReference; routeParams.ReturnDirections = true; routeParams.DirectionsLengthUnit = LinearUnits.Kilometers; routeParams.DirectionsLanguage = new System.Globalization.CultureInfo("en"); List <Graphic> graphicsStops = new List <Graphic>(); graphicsStops.Add(new Graphic(startLocation)); graphicsStops.Add(new Graphic(endLocation)); FeaturesAsFeature stops = new FeaturesAsFeature(graphicsStops); stops.SpatialReference = mapView1.SpatialReference; routeParams.Stops = stops; RouteResult routeResult = await _routeTask.SolveAsync(routeParams); if (routeResult.Routes.Count > 0) { Graphic graphic = new Graphic(routeResult.Routes[0].RouteGraphic.Geometry); _routeGraphicsLayer.Graphics.Add(graphic); List <string> directions = new List <string>(); foreach (var item in routeResult.Routes[0].RouteDirections) { directions.Add(" " + item.Text + "\r\n"); } Directions = String.Join <string>(String.Empty, directions); } } catch (AggregateException ex) { var innermostExceptions = ex.Flatten().InnerExceptions; if (innermostExceptions != null && innermostExceptions.Count > 0) { MessageBox.Show(innermostExceptions[0].Message); } else { MessageBox.Show(ex.Message); } } catch (System.Exception ex) { MessageBox.Show("Error: " + ex.Message); } }