// 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) { var path = System.IO.Path.Combine(ApplicationData.Current.LocalFolder.Path, _localRoutingDatabase); _routeTask = await Task.Run<LocalRouteTask>(() => new LocalRouteTask(path, _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 Exception("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) { var _x = new MessageDialog(innermostExceptions[0].Message, "Sample Error").ShowAsync(); } else { var _x = new MessageDialog(ex.Message, "Sample Error").ShowAsync(); } } catch (System.Exception ex) { var _x = new MessageDialog(ex.Message, "Sample Error").ShowAsync(); } finally { progress.Visibility = Visibility.Collapsed; if (_directionsOverlay.Graphics.Count() > 0) panelResults.Visibility = Visibility.Visible; } }
/// <summary> /// Initializes a new instance of the MainViewModel class. /// </summary> public MainViewModel() { if (IsInDesignMode) { // Code runs in Blend --> create design time data. } else { // Code runs "for real" ConfigService config = new ConfigService(); this.myModel = config.LoadJSON(); this.AddStopsRelayCommand = new RelayCommand(AddStops); Messenger.Default.Register<Esri.ArcGISRuntime.Controls.MapView>(this, (mapView) => { this.mapView = mapView; this.mapView.MaxScale = 500; ArcGISLocalTiledLayer localTiledLayer = new ArcGISLocalTiledLayer(this.TilePackage); localTiledLayer.ID = "SF Basemap"; localTiledLayer.InitializeAsync(); this.mapView.Map.Layers.Add(localTiledLayer); this.CreateLocalServiceAndDynamicLayer(); this.CreateFeatureLayers(); //============================ // Create a new renderer to symbolize the routing polyline and apply it to the GraphicsLayer SimpleLineSymbol polylineRouteSymbol = new SimpleLineSymbol(); polylineRouteSymbol.Color = System.Windows.Media.Colors.Red; polylineRouteSymbol.Style = Esri.ArcGISRuntime.Symbology.SimpleLineStyle.Solid; polylineRouteSymbol.Width = 4; SimpleRenderer polylineRouteRenderer = new SimpleRenderer(); polylineRouteRenderer.Symbol = polylineRouteSymbol; // Create a new renderer to symbolize the start and end points that define the route and apply it to the GraphicsLayer SimpleMarkerSymbol stopSymbol = new SimpleMarkerSymbol(); stopSymbol.Color = System.Windows.Media.Colors.Green; stopSymbol.Size = 12; stopSymbol.Style = SimpleMarkerStyle.Circle; SimpleRenderer stopRenderer = new SimpleRenderer(); stopRenderer.Symbol = stopSymbol; // create the route results graphics layer this.routeGraphicsLayer = new GraphicsLayer(); this.routeGraphicsLayer.ID = "RouteResults"; this.routeGraphicsLayer.DisplayName = "Routes"; this.routeGraphicsLayer.Renderer = polylineRouteRenderer; this.routeGraphicsLayer.InitializeAsync(); this.mapView.Map.Layers.Add(this.routeGraphicsLayer); // create the stops graphics layer this.stopsGraphicsLayer = new GraphicsLayer(); this.stopsGraphicsLayer.ID = "Stops"; this.stopsGraphicsLayer.DisplayName = "Stops"; this.stopsGraphicsLayer.Renderer = stopRenderer; this.stopsGraphicsLayer.InitializeAsync(); this.mapView.Map.Layers.Add(this.stopsGraphicsLayer); // Offline routing task. routeTask = new LocalRouteTask(@"C:\ArcGISRuntimeBook\Data\Networks\RuntimeSanFrancisco.geodatabase", "Streets_ND"); }); } }
// 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; } } }
/// <summary> /// Initializes a new instance of the MainViewModel class. /// </summary> public MainViewModel() { if (IsInDesignMode) { // Code runs in Blend --> create design time data. } else { // Code runs "for real" ConfigService config = new ConfigService(); this.myModel = config.LoadJSON(); this.AddStopsRelayCommand = new RelayCommand(AddStops); Messenger.Default.Register <Esri.ArcGISRuntime.Controls.MapView>(this, (mapView) => { this.mapView = mapView; this.mapView.MaxScale = 500; ArcGISLocalTiledLayer localTiledLayer = new ArcGISLocalTiledLayer(this.TilePackage); localTiledLayer.ID = "SF Basemap"; localTiledLayer.InitializeAsync(); this.mapView.Map.Layers.Add(localTiledLayer); this.CreateLocalServiceAndDynamicLayer(); this.CreateFeatureLayers(); //============================ // Create a new renderer to symbolize the routing polyline and apply it to the GraphicsLayer SimpleLineSymbol polylineRouteSymbol = new SimpleLineSymbol(); polylineRouteSymbol.Color = System.Windows.Media.Colors.Red; polylineRouteSymbol.Style = Esri.ArcGISRuntime.Symbology.SimpleLineStyle.Solid; polylineRouteSymbol.Width = 4; SimpleRenderer polylineRouteRenderer = new SimpleRenderer(); polylineRouteRenderer.Symbol = polylineRouteSymbol; // Create a new renderer to symbolize the start and end points that define the route and apply it to the GraphicsLayer SimpleMarkerSymbol stopSymbol = new SimpleMarkerSymbol(); stopSymbol.Color = System.Windows.Media.Colors.Green; stopSymbol.Size = 12; stopSymbol.Style = SimpleMarkerStyle.Circle; SimpleRenderer stopRenderer = new SimpleRenderer(); stopRenderer.Symbol = stopSymbol; // create the route results graphics layer this.routeGraphicsLayer = new GraphicsLayer(); this.routeGraphicsLayer.ID = "RouteResults"; this.routeGraphicsLayer.DisplayName = "Routes"; this.routeGraphicsLayer.Renderer = polylineRouteRenderer; this.routeGraphicsLayer.InitializeAsync(); this.mapView.Map.Layers.Add(this.routeGraphicsLayer); // create the stops graphics layer this.stopsGraphicsLayer = new GraphicsLayer(); this.stopsGraphicsLayer.ID = "Stops"; this.stopsGraphicsLayer.DisplayName = "Stops"; this.stopsGraphicsLayer.Renderer = stopRenderer; this.stopsGraphicsLayer.InitializeAsync(); this.mapView.Map.Layers.Add(this.stopsGraphicsLayer); // Offline routing task. routeTask = new LocalRouteTask(@"C:\ArcGISRuntimeBook\Data\Networks\RuntimeSanFrancisco.geodatabase", "Streets_ND"); }); } }