private async void mapView1_Tapped(object sender, Esri.ArcGISRuntime.Controls.MapViewInputEventArgs e) { var mp = e.Location; Graphic stop = new Graphic() { Geometry = mp }; var stopsGraphicsLayer = mapView1.Map.Layers["MyStopsGraphicsLayer"] as GraphicsLayer; stopsGraphicsLayer.Graphics.Add(stop); if (stopsGraphicsLayer.Graphics.Count > 1) { try { var routeTask = new OnlineRouteTask(new Uri("http://tasks.arcgisonline.com/ArcGIS/rest/services/NetworkAnalysis/ESRI_Route_NA/NAServer/Route")); var routeParams = await routeTask.GetDefaultParametersAsync(); FeaturesAsFeature stopsFeatures = new FeaturesAsFeature(); stopsFeatures.Features = stopsGraphicsLayer.Graphics; routeParams.Stops = stopsFeatures; routeParams.UseTimeWindows = false; routeParams.OutSpatialReference = mapView1.SpatialReference; routeParams.DirectionsLengthUnit = LinearUnits.Miles; var result = await routeTask.SolveAsync(routeParams); if (result != null) { if (result.Routes != null && result.Routes.Count > 0) { var firstRoute = result.Routes.FirstOrDefault(); var direction = firstRoute.RouteDirections.FirstOrDefault(); if (direction != null) { int totalMins = 0; foreach (RouteDirection dir in firstRoute.RouteDirections) { totalMins = totalMins + dir.Time.Minutes; } await new MessageDialog(string.Format("{0:N2} minutes", totalMins)).ShowAsync(); } var routeLayer = mapView1.Map.Layers["MyRouteGraphicsLayer"] as GraphicsLayer; routeLayer.Graphics.Add(firstRoute.RouteGraphic); } } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); } } }
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 void mapView1_Tapped(object sender, Esri.ArcGISRuntime.Controls.MapViewInputEventArgs e) { var mp = e.Location; Graphic stop = new Graphic() { Geometry = mp }; var stopsGraphicsLayer = mapView1.Map.Layers["MyStopsGraphicsLayer"] as GraphicsLayer; stopsGraphicsLayer.Graphics.Add(stop); if (stopsGraphicsLayer.Graphics.Count > 1) { try { var routeTask = new OnlineRouteTask(new Uri("http://tasks.arcgisonline.com/ArcGIS/rest/services/NetworkAnalysis/ESRI_Route_NA/NAServer/Route")); var routeParams = await routeTask.GetDefaultParametersAsync(); FeaturesAsFeature stopsFeatures = new FeaturesAsFeature(); stopsFeatures.Features = stopsGraphicsLayer.Graphics; routeParams.Stops = stopsFeatures; routeParams.UseTimeWindows = false; routeParams.OutSpatialReference = mapView1.SpatialReference; routeParams.DirectionsLengthUnit = LinearUnits.Miles; var result = await routeTask.SolveAsync(routeParams); if (result != null) { if (result.Routes != null && result.Routes.Count > 0) { var firstRoute = result.Routes.FirstOrDefault(); var direction = firstRoute.RouteDirections.FirstOrDefault(); if (direction != null) { int totalMins = 0; foreach (RouteDirection dir in firstRoute.RouteDirections) totalMins = totalMins + dir.Time.Minutes; await new MessageDialog(string.Format("{0:N2} minutes", totalMins)).ShowAsync(); } var routeLayer = mapView1.Map.Layers["MyRouteGraphicsLayer"] as GraphicsLayer; routeLayer.Graphics.Add(firstRoute.RouteGraphic); } } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); } } }
private async void OnSolveRouteClicked(object sender, RoutedEventArgs e) { var stopsLayer = mapView1.Map.Layers["MyStopsGraphicsLayer"] as GraphicsLayer; var barriersLayer = mapView1.Map.Layers["MyBarriersGraphicsLayer"] as GraphicsLayer; if (stopsLayer.Graphics.Count > 1) { try { OnlineRouteTask routeTask = new OnlineRouteTask(new Uri("http://tasks.arcgisonline.com/ArcGIS/rest/services/NetworkAnalysis/ESRI_Route_NA/NAServer/Route")); RouteParameters routeParams = await routeTask.GetDefaultParametersAsync(); FeaturesAsFeature featureAsFeature = new FeaturesAsFeature(); featureAsFeature.Features = stopsLayer.Graphics; routeParams.Stops = featureAsFeature; routeParams.UseTimeWindows = false; routeParams.OutSpatialReference = mapView1.SpatialReference; FeaturesAsFeature barrierFeatures = new FeaturesAsFeature(); barrierFeatures.Features = barriersLayer.Graphics; routeParams.PointBarriers = barrierFeatures; routeParams.OutputGeometryPrecision = 1; //routeParams.OutputGeometryPrecisionUnit = LinearUnits.Miles; routeParams.DirectionsLengthUnit = LinearUnits.Miles; var result = await routeTask.SolveAsync(routeParams); if (result != null) { GraphicsLayer routeLayer = mapView1.Map.Layers["MyRouteGraphicsLayer"] as GraphicsLayer; routeLayer.Graphics.Clear(); foreach (var route in result.Routes) { routeLayer.Graphics.Add(route.RouteGraphic); } } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); } } }
private async void OnSolveRouteClicked(object sender, RoutedEventArgs e) { var stopsLayer = mapView1.Map.Layers["MyStopsGraphicsLayer"] as GraphicsLayer; var barriersLayer = mapView1.Map.Layers["MyBarriersGraphicsLayer"] as GraphicsLayer; if (stopsLayer.Graphics.Count > 1) { try { OnlineRouteTask routeTask = new OnlineRouteTask(new Uri("http://tasks.arcgisonline.com/ArcGIS/rest/services/NetworkAnalysis/ESRI_Route_NA/NAServer/Route")); RouteParameters routeParams = await routeTask.GetDefaultParametersAsync(); FeaturesAsFeature featureAsFeature = new FeaturesAsFeature(); featureAsFeature.Features = stopsLayer.Graphics; routeParams.Stops = featureAsFeature; routeParams.UseTimeWindows = false; routeParams.OutSpatialReference = mapView1.SpatialReference; FeaturesAsFeature barrierFeatures = new FeaturesAsFeature(); barrierFeatures.Features = barriersLayer.Graphics; routeParams.PointBarriers = barrierFeatures; routeParams.OutputGeometryPrecision = 1; //routeParams.OutputGeometryPrecisionUnit = LinearUnits.Miles; routeParams.DirectionsLengthUnit = LinearUnits.Miles; var result = await routeTask.SolveAsync(routeParams); if (result != null) { GraphicsLayer routeLayer = mapView1.Map.Layers["MyRouteGraphicsLayer"] as GraphicsLayer; routeLayer.Graphics.Clear(); foreach (var route in result.Routes) routeLayer.Graphics.Add(route.RouteGraphic); } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); } } }
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); } }
private async Task CalculateRoute() { _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 = false; routeParams.DirectionsLengthUnit = LinearUnits.Kilometers; 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); } } 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 { } }
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 void SolveButton_Click(object sender, RoutedEventArgs e) { try { if (_stops.Count == 0) return; RouteParameters routeParams = await _onlineRouteTask.GetDefaultParametersAsync(); GenerateBarriers(); FeaturesAsFeature stopsFeatures = new FeaturesAsFeature(); stopsFeatures.Features = _stops; routeParams.Stops = stopsFeatures; if (_polylineBarriers.Count > 0) { FeaturesAsFeature polylineBarrierFeatures = new FeaturesAsFeature(); polylineBarrierFeatures.Features = _polylineBarriers; routeParams.PolylineBarriers = polylineBarrierFeatures; } if (_polygonBarriers.Count > 0) { FeaturesAsFeature polygonBarrierFeatures = new FeaturesAsFeature(); polygonBarrierFeatures.Features = _polygonBarriers; routeParams.PolygonBarriers = polygonBarrierFeatures; } List<AttributeParameterValue> aps = new List<AttributeParameterValue>(); AttributeParameterValue apv = GetAttributeParameterValue(AttributeParameter2.SelectionBoxItem.ToString().Trim()); if (apv != null) aps.Add(apv); //routeParams.AttributeParameterValues = aps; //routeParams.ReturnDirections = ReturnDirections2.IsChecked.HasValue ? ReturnDirections2.IsChecked.Value : false; routeParams.DirectionsLanguage = String.IsNullOrEmpty(DirectionsLanguage2.Text) ? new System.Globalization.CultureInfo("en-US") : new System.Globalization.CultureInfo(DirectionsLanguage2.Text); routeParams.DirectionsLengthUnit = GetDirectionsLengthUnits(DirectionsLengthUnits2.SelectionBoxItem.ToString().Trim()); routeParams.ReturnRoutes = ReturnRoutes2.IsChecked.HasValue ? ReturnRoutes2.IsChecked.Value : false; routeParams.ReturnStops = ReturnFacilities2.IsChecked.HasValue ? ReturnFacilities2.IsChecked.Value : false; routeParams.ReturnPointBarriers = ReturnBarriers2.IsChecked.HasValue ? ReturnBarriers2.IsChecked.Value : false; routeParams.ReturnPolygonBarriers = ReturnPolygonBarriers2.IsChecked.HasValue ? ReturnPolygonBarriers2.IsChecked.Value : false; routeParams.ReturnPolylineBarriers = ReturnPolylineBarriers2.IsChecked.HasValue ? ReturnPolylineBarriers2.IsChecked.Value : false; routeParams.OutputLines = GetOutputLines(OutputLines2.SelectionBoxItem.ToString().Trim()); routeParams.OutSpatialReference = string.IsNullOrEmpty(OutputSpatialReference2.Text) ? mapView1.SpatialReference : new SpatialReference(int.Parse(OutputSpatialReference2.Text)); //routeParams.AccumulateAttributeNames = AccumulateAttributeNames2.Text.Split(','); routeParams.ImpedanceAttributeName = ImpedanceAttributeName2.Text; routeParams.RestrictionAttributeNames = RestrictionAttributeNames2.Text.Split(','); routeParams.RestrictUTurns = GetRestrictUTurns(RestrictUTurns2.SelectionBoxItem.ToString().Trim()); routeParams.UseHierarchy = UseHierarchy2.IsChecked.HasValue ? UseHierarchy2.IsChecked.Value : false; routeParams.OutputGeometryPrecision = string.IsNullOrEmpty(OutputGeometryPrecision2.Text) ? 0 : double.Parse(OutputGeometryPrecision2.Text); routeParams.OutputGeometryPrecisionUnit = GetGeometryPrecisionUnits(OutputGeometryPrecisionUnits2.SelectionBoxItem.ToString().Trim()); RouteResult result= await _onlineRouteTask.SolveAsync(routeParams); _routeGraphicsLayer.Graphics.Clear(); foreach (Route route in result.Routes) { Graphic g = route.RouteGraphic; g.Symbol = LayoutRoot.Resources["RouteSymbol"] as SimpleLineSymbol; _routeGraphicsLayer.Graphics.Add(g); } } catch (Exception ex) { var dlg = new MessageDialog(ex.Message, "Solve Failed!"); var _ = dlg.ShowAsync(); } }
private async void SolveButton_Click(object sender, RoutedEventArgs e) { try { if (_stops.Count == 0) { return; } RouteParameters routeParams = await _onlineRouteTask.GetDefaultParametersAsync(); GenerateBarriers(); FeaturesAsFeature stopsFeatures = new FeaturesAsFeature(); stopsFeatures.Features = _stops; routeParams.Stops = stopsFeatures; if (_polylineBarriers.Count > 0) { FeaturesAsFeature polylineBarrierFeatures = new FeaturesAsFeature(); polylineBarrierFeatures.Features = _polylineBarriers; routeParams.PolylineBarriers = polylineBarrierFeatures; } if (_polygonBarriers.Count > 0) { FeaturesAsFeature polygonBarrierFeatures = new FeaturesAsFeature(); polygonBarrierFeatures.Features = _polygonBarriers; routeParams.PolygonBarriers = polygonBarrierFeatures; } List <AttributeParameterValue> aps = new List <AttributeParameterValue>(); AttributeParameterValue apv = GetAttributeParameterValue(AttributeParameter2.SelectionBoxItem.ToString().Trim()); if (apv != null) { aps.Add(apv); } //routeParams.AttributeParameterValues = aps; //routeParams.ReturnDirections = ReturnDirections2.IsChecked.HasValue ? ReturnDirections2.IsChecked.Value : false; routeParams.DirectionsLanguage = String.IsNullOrEmpty(DirectionsLanguage2.Text) ? new System.Globalization.CultureInfo("en-US") : new System.Globalization.CultureInfo(DirectionsLanguage2.Text); routeParams.DirectionsLengthUnit = GetDirectionsLengthUnits(DirectionsLengthUnits2.SelectionBoxItem.ToString().Trim()); routeParams.ReturnRoutes = ReturnRoutes2.IsChecked.HasValue ? ReturnRoutes2.IsChecked.Value : false; routeParams.ReturnStops = ReturnFacilities2.IsChecked.HasValue ? ReturnFacilities2.IsChecked.Value : false; routeParams.ReturnPointBarriers = ReturnBarriers2.IsChecked.HasValue ? ReturnBarriers2.IsChecked.Value : false; routeParams.ReturnPolygonBarriers = ReturnPolygonBarriers2.IsChecked.HasValue ? ReturnPolygonBarriers2.IsChecked.Value : false; routeParams.ReturnPolylineBarriers = ReturnPolylineBarriers2.IsChecked.HasValue ? ReturnPolylineBarriers2.IsChecked.Value : false; routeParams.OutputLines = GetOutputLines(OutputLines2.SelectionBoxItem.ToString().Trim()); routeParams.OutSpatialReference = string.IsNullOrEmpty(OutputSpatialReference2.Text) ? mapView1.SpatialReference : new SpatialReference(int.Parse(OutputSpatialReference2.Text)); //routeParams.AccumulateAttributeNames = AccumulateAttributeNames2.Text.Split(','); routeParams.ImpedanceAttributeName = ImpedanceAttributeName2.Text; routeParams.RestrictionAttributeNames = RestrictionAttributeNames2.Text.Split(','); routeParams.RestrictUTurns = GetRestrictUTurns(RestrictUTurns2.SelectionBoxItem.ToString().Trim()); routeParams.UseHierarchy = UseHierarchy2.IsChecked.HasValue ? UseHierarchy2.IsChecked.Value : false; routeParams.OutputGeometryPrecision = string.IsNullOrEmpty(OutputGeometryPrecision2.Text) ? 0 : double.Parse(OutputGeometryPrecision2.Text); routeParams.OutputGeometryPrecisionUnit = GetGeometryPrecisionUnits(OutputGeometryPrecisionUnits2.SelectionBoxItem.ToString().Trim()); RouteResult result = await _onlineRouteTask.SolveAsync(routeParams); _routeGraphicsLayer.Graphics.Clear(); foreach (Route route in result.Routes) { Graphic g = route.RouteGraphic; g.Symbol = LayoutRoot.Resources["RouteSymbol"] as SimpleLineSymbol; _routeGraphicsLayer.Graphics.Add(g); } } catch (Exception ex) { var dlg = new MessageDialog(ex.Message, "Solve Failed!"); var _ = dlg.ShowAsync(); } }