private void routeTask_SolveCompleted(object sender, RouteEventArgs e)
        {
            GraphicsLayer graphicsLayer = MyMap.Layers["MyRouteGraphicsLayer"] as GraphicsLayer;
            RouteResult   routeResult   = e.RouteResults[0];

            _directionsFeatureSet = routeResult.Directions;

            graphicsLayer.Graphics.Add(new Graphic()
            {
                Geometry = _directionsFeatureSet.MergedGeometry, Symbol = LayoutRoot.Resources["RouteSymbol"] as ESRI.ArcGIS.Client.Symbols.Symbol
            });
            TotalDistanceTextBlock.Text = string.Format("Total Distance: {0}", FormatDistance(_directionsFeatureSet.TotalLength, "miles"));
            TotalTimeTextBlock.Text     = string.Format("Total Time: {0}", FormatTime(_directionsFeatureSet.TotalTime));
            TitleTextBlock.Text         = _directionsFeatureSet.RouteName;

            int i = 1;

            foreach (Graphic graphic in _directionsFeatureSet.Features)
            {
                System.Text.StringBuilder text = new System.Text.StringBuilder();
                text.AppendFormat("{0}. {1}", i, graphic.Attributes["text"]);
                if (i > 1 && i < _directionsFeatureSet.Features.Count)
                {
                    string distance = FormatDistance(Convert.ToDouble(graphic.Attributes["length"]), "miles");
                    string time     = null;
                    if (graphic.Attributes.ContainsKey("time"))
                    {
                        time = FormatTime(Convert.ToDouble(graphic.Attributes["time"]));
                    }
                    if (!string.IsNullOrEmpty(distance) || !string.IsNullOrEmpty(time))
                    {
                        text.Append(" (");
                    }
                    text.Append(distance);
                    if (!string.IsNullOrEmpty(distance) && !string.IsNullOrEmpty(time))
                    {
                        text.Append(", ");
                    }
                    text.Append(time);
                    if (!string.IsNullOrEmpty(distance) || !string.IsNullOrEmpty(time))
                    {
                        text.Append(")");
                    }
                }
                TextBlock textBlock = new TextBlock()
                {
                    Text = text.ToString(), Tag = graphic, Margin = new Thickness(4), Cursor = Cursors.Hand
                };
                textBlock.MouseLeftButtonDown += new MouseButtonEventHandler(directionsSegment_MouseLeftButtonDown);
                DirectionsStackPanel.Children.Add(textBlock);
                i++;
            }
            MyMap.ZoomTo(Expand(_directionsFeatureSet.Extent));
        }
        // ***********************************************************************************
        // * Initialize Find Closest Facility Result View
        // ***********************************************************************************
        public FindCloseFacilityResultView(FindClosestResourceToolbar findClosestToolbar, RouteResult[] routeResults, MapWidget mapWidget)
        {
            InitializeComponent();

            base.DataContext = this;

            // Store a reference to the MapWidget that the toolbar has been installed to.
            _mapWidget = mapWidget;

            _dirSummary             = new DirectionSummary();
            _dirSummary.From        = "From";
            _dirSummary.To          = "To";
            _dirSummary.FieldType   = "FieldType";
            _dirSummary.Rank        = "Rank";
            _dirSummary.TotalTime   = "Total Time";
            _dirSummary.TotalLength = "Total Length";
            _directions.Add(_dirSummary);

            _closestFacilityToolbar = findClosestToolbar;

            // add each route result to the result dialog.
            foreach (RouteResult routeResult in routeResults)
            {
                DirectionsFeatureSet directionsFS = routeResult.Directions;
                string routeName = directionsFS.RouteName;
                int    j         = routeName.IndexOf("-");

                _dirSummary               = new DirectionSummary();
                _dirSummary.From          = routeName.Substring(0, j - 1);
                _dirSummary.To            = routeName.Substring(j + 2);
                _dirSummary.FieldType     = _closestFacilityToolbar.FacilityType;
                _dirSummary.Rank          = directionsFS.RouteID.ToString();
                _dirSummary.TotalTime     = directionsFS.TotalDriveTime.ToString("0.0") + " minutes";
                _dirSummary.TotalLength   = directionsFS.TotalLength.ToString("0.0") + " miles";
                _dirSummary.SelectedRoute = routeResult;
                _directions.Add(_dirSummary);
            }
        }
        private async void GetDirections_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (_cts != null)
                    _cts.Cancel();

                _cts = new CancellationTokenSource();

                DirectionsStackPanel.Children.Clear();
                _stops.Clear();
                _routeGraphicsLayer.Graphics.Clear();


                _activeSegmentGraphic = null;

                //Geocode from address
                LocatorTaskFindResult fromAddress = await _locator.FindTaskAsync(ParseSearchText(FromTextBox.Text), _cts.Token);
                // if no result?
                Graphic fromLocation = fromAddress.Result.Locations[0].Graphic;
                fromLocation.Geometry.SpatialReference = MyMap.SpatialReference;
                fromLocation.Attributes.Add("name", fromAddress.Result.Locations[0].Name);
                _stops.Add(fromLocation);

                fromLocation.Symbol = LayoutRoot.Resources["FromSymbol"] as ESRI.ArcGIS.Client.Symbols.Symbol;
                _routeGraphicsLayer.Graphics.Add(fromLocation);

                //Geocode to address
                LocatorTaskFindResult toAddress = await _locator.FindTaskAsync(ParseSearchText(ToTextBox.Text), _cts.Token);
                Graphic toLocation = toAddress.Result.Locations[0].Graphic;
                toLocation.Geometry.SpatialReference = MyMap.SpatialReference;
                toLocation.Attributes.Add("name", toAddress.Result.Locations[0].Name);
                _stops.Add(toLocation);

                toLocation.Symbol = LayoutRoot.Resources["ToSymbol"] as ESRI.ArcGIS.Client.Symbols.Symbol;
                _routeGraphicsLayer.Graphics.Add(toLocation);

                //Get route between from and to
                _routeParams.OutSpatialReference = MyMap.SpatialReference;
                SolveRouteResult solveRouteResult = await _routeTask.SolveTaskAsync(_routeParams, _cts.Token);

                RouteResult routeResult = solveRouteResult.RouteResults[0];
                _directionsFeatureSet = routeResult.Directions;

                _routeGraphicsLayer.Graphics.Add(new Graphic() { Geometry = _directionsFeatureSet.MergedGeometry, Symbol = LayoutRoot.Resources["RouteSymbol"] as ESRI.ArcGIS.Client.Symbols.Symbol });
                TotalDistanceTextBlock.Text = string.Format("Total Distance: {0}", FormatDistance(_directionsFeatureSet.TotalLength, "miles"));
                TotalTimeTextBlock.Text = string.Format("Total Time: {0}", FormatTime(_directionsFeatureSet.TotalTime));
                TitleTextBlock.Text = _directionsFeatureSet.RouteName;

                int i = 1;
                foreach (Graphic graphic in _directionsFeatureSet.Features)
                {
                    System.Text.StringBuilder text = new System.Text.StringBuilder();
                    text.AppendFormat("{0}. {1}", i, graphic.Attributes["text"]);
                    if (i > 1 && i < _directionsFeatureSet.Features.Count)
                    {
                        string distance = FormatDistance(Convert.ToDouble(graphic.Attributes["length"]), "miles");
                        string time = null;
                        if (graphic.Attributes.ContainsKey("time"))
                        {
                            time = FormatTime(Convert.ToDouble(graphic.Attributes["time"]));
                        }
                        if (!string.IsNullOrEmpty(distance) || !string.IsNullOrEmpty(time))
                            text.Append(" (");
                        text.Append(distance);
                        if (!string.IsNullOrEmpty(distance) && !string.IsNullOrEmpty(time))
                            text.Append(", ");
                        text.Append(time);
                        if (!string.IsNullOrEmpty(distance) || !string.IsNullOrEmpty(time))
                            text.Append(")");
                    }
                    TextBlock textBlock = new TextBlock() { Text = text.ToString(), Tag = graphic, Margin = new Thickness(4), Cursor = Cursors.Hand };
                    textBlock.MouseLeftButtonDown += new MouseButtonEventHandler(directionsSegment_MouseLeftButtonDown);
                    DirectionsStackPanel.Children.Add(textBlock);
                    i++;
                }
                MyMap.ZoomTo(Expand(_directionsFeatureSet.Extent));
            }
            catch (Exception ex)
            {
                if (ex is ServiceException)
                {
                    MessageBox.Show(String.Format("{0}: {1}", (ex as ServiceException).Code.ToString(), (ex as ServiceException).Details[0]), "Error", MessageBoxButton.OK);
                    return;
                }
            }
        }
        private void routeTask_SolveCompleted(object sender, RouteEventArgs e)
        {
            GraphicsLayer graphicsLayer = MyMap.Layers["MyRouteGraphicsLayer"] as GraphicsLayer;
            RouteResult routeResult = e.RouteResults[0];
            _directionsFeatureSet = routeResult.Directions;

            graphicsLayer.Graphics.Add(new Graphic() { Geometry = _directionsFeatureSet.MergedGeometry, Symbol = LayoutRoot.Resources["RouteSymbol"] as ESRI.ArcGIS.Client.Symbols.Symbol });
            TotalDistanceTextBlock.Text = string.Format("Total Distance: {0}", FormatDistance(_directionsFeatureSet.TotalLength, "miles"));
            TotalTimeTextBlock.Text = string.Format("Total Time: {0}", FormatTime(_directionsFeatureSet.TotalTime));
            TitleTextBlock.Text = _directionsFeatureSet.RouteName;

            int i = 1;
            foreach (Graphic graphic in _directionsFeatureSet.Features)
            {
                System.Text.StringBuilder text = new System.Text.StringBuilder();
                text.AppendFormat("{0}. {1}", i, graphic.Attributes["text"]);
                if (i > 1 && i < _directionsFeatureSet.Features.Count)
                {
                    string distance = FormatDistance(Convert.ToDouble(graphic.Attributes["length"]), "miles");
                    string time = null;
                    if (graphic.Attributes.ContainsKey("time"))
                    {
                        time = FormatTime(Convert.ToDouble(graphic.Attributes["time"]));
                    }
                    if (!string.IsNullOrEmpty(distance) || !string.IsNullOrEmpty(time))
                        text.Append(" (");
                    text.Append(distance);
                    if (!string.IsNullOrEmpty(distance) && !string.IsNullOrEmpty(time))
                        text.Append(", ");
                    text.Append(time);
                    if (!string.IsNullOrEmpty(distance) || !string.IsNullOrEmpty(time))
                        text.Append(")");
                }
                TextBlock textBlock = new TextBlock() { Text = text.ToString(), Tag = graphic, Margin = new Thickness(4), Cursor = Cursors.Hand };
                textBlock.MouseLeftButtonDown += new MouseButtonEventHandler(directionsSegment_MouseLeftButtonDown);
                DirectionsStackPanel.Children.Add(textBlock);
                i++;
            }
            MyMap.ZoomTo(Expand(_directionsFeatureSet.Extent));
        }
        private async void GetDirections_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (_cts != null)
                {
                    _cts.Cancel();
                }

                _cts = new CancellationTokenSource();

                DirectionsStackPanel.Children.Clear();
                _stops.Clear();
                _routeGraphicsLayer.Graphics.Clear();


                _activeSegmentGraphic = null;

                //Geocode from address
                LocatorTaskFindResult fromAddress = await _locator.FindTaskAsync(ParseSearchText(FromTextBox.Text), _cts.Token);

                // if no result?
                Graphic fromLocation = fromAddress.Result.Locations[0].Graphic;
                fromLocation.Geometry.SpatialReference = MyMap.SpatialReference;
                fromLocation.Attributes.Add("name", fromAddress.Result.Locations[0].Name);
                _stops.Add(fromLocation);

                fromLocation.Symbol = LayoutRoot.Resources["FromSymbol"] as ESRI.ArcGIS.Client.Symbols.Symbol;
                _routeGraphicsLayer.Graphics.Add(fromLocation);

                //Geocode to address
                LocatorTaskFindResult toAddress = await _locator.FindTaskAsync(ParseSearchText(ToTextBox.Text), _cts.Token);

                Graphic toLocation = toAddress.Result.Locations[0].Graphic;
                toLocation.Geometry.SpatialReference = MyMap.SpatialReference;
                toLocation.Attributes.Add("name", toAddress.Result.Locations[0].Name);
                _stops.Add(toLocation);

                toLocation.Symbol = LayoutRoot.Resources["ToSymbol"] as ESRI.ArcGIS.Client.Symbols.Symbol;
                _routeGraphicsLayer.Graphics.Add(toLocation);

                //Get route between from and to
                _routeParams.OutSpatialReference = MyMap.SpatialReference;
                SolveRouteResult solveRouteResult = await _routeTask.SolveTaskAsync(_routeParams, _cts.Token);

                RouteResult routeResult = solveRouteResult.RouteResults[0];
                _directionsFeatureSet = routeResult.Directions;

                _routeGraphicsLayer.Graphics.Add(new Graphic()
                {
                    Geometry = _directionsFeatureSet.MergedGeometry, Symbol = LayoutRoot.Resources["RouteSymbol"] as ESRI.ArcGIS.Client.Symbols.Symbol
                });
                TotalDistanceTextBlock.Text = string.Format("Total Distance: {0}", FormatDistance(_directionsFeatureSet.TotalLength, "miles"));
                TotalTimeTextBlock.Text     = string.Format("Total Time: {0}", FormatTime(_directionsFeatureSet.TotalTime));
                TitleTextBlock.Text         = _directionsFeatureSet.RouteName;

                int i = 1;
                foreach (Graphic graphic in _directionsFeatureSet.Features)
                {
                    System.Text.StringBuilder text = new System.Text.StringBuilder();
                    text.AppendFormat("{0}. {1}", i, graphic.Attributes["text"]);
                    if (i > 1 && i < _directionsFeatureSet.Features.Count)
                    {
                        string distance = FormatDistance(Convert.ToDouble(graphic.Attributes["length"]), "miles");
                        string time     = null;
                        if (graphic.Attributes.ContainsKey("time"))
                        {
                            time = FormatTime(Convert.ToDouble(graphic.Attributes["time"]));
                        }
                        if (!string.IsNullOrEmpty(distance) || !string.IsNullOrEmpty(time))
                        {
                            text.Append(" (");
                        }
                        text.Append(distance);
                        if (!string.IsNullOrEmpty(distance) && !string.IsNullOrEmpty(time))
                        {
                            text.Append(", ");
                        }
                        text.Append(time);
                        if (!string.IsNullOrEmpty(distance) || !string.IsNullOrEmpty(time))
                        {
                            text.Append(")");
                        }
                    }
                    TextBlock textBlock = new TextBlock()
                    {
                        Text = text.ToString(), Tag = graphic, Margin = new Thickness(4), Cursor = Cursors.Hand
                    };
                    textBlock.MouseLeftButtonDown += new MouseButtonEventHandler(directionsSegment_MouseLeftButtonDown);
                    DirectionsStackPanel.Children.Add(textBlock);
                    i++;
                }
                MyMap.ZoomTo(Expand(_directionsFeatureSet.Extent));
            }
            catch (Exception ex)
            {
                if (ex is ServiceException)
                {
                    MessageBox.Show(String.Format("{0}: {1}", (ex as ServiceException).Code.ToString(), (ex as ServiceException).Details[0]), "Error", MessageBoxButton.OK);
                    return;
                }
            }
        }
 private void UpdateDirectionsPanel(DirectionsFeatureSet featureSet)
 {
     TotalDistanceTextBlock.Text = string.Format("Total Distance: {0}", FormatDistance(featureSet.TotalLength, "miles"));
     TotalTimeTextBlock.Text = string.Format("Total Time: {0}", FormatTime(featureSet.TotalTime));
     TitleTextBlock.Text = featureSet.RouteName;
     int i = 1;
     foreach (Graphic graphic in featureSet.Features)
     {
         StringBuilder sb = new StringBuilder();
         sb.AppendFormat("{0}. {1}", i, graphic.Attributes["text"]);
         if (i > 1 && i < featureSet.Features.Count)
         {
             string distance = FormatDistance(Convert.ToDouble(graphic.Attributes["length"]), "miles");
             string time = null;
             if (graphic.Attributes.ContainsKey("time"))
                 time = FormatTime(Convert.ToDouble(graphic.Attributes["time"]));
             sb.AppendLine();
             if (!string.IsNullOrEmpty(distance) || !string.IsNullOrEmpty(time))
                 sb.Append(" (");
             sb.Append(distance);
             if (!string.IsNullOrEmpty(distance) && !string.IsNullOrEmpty(time))
                 sb.Append(", ");
             sb.Append(time);
             if (!string.IsNullOrEmpty(distance) || !string.IsNullOrEmpty(time))
                 sb.Append(")");
         }
         TextBlock textBlock = new TextBlock() { Text = sb.ToString(), Tag = graphic, Margin = new Thickness(4) };
         textBlock.MouseLeftButtonDown += DirectionsTextBlock_MouseLeftButtonDown;
         DirectionsStackPanel.Children.Add(textBlock);
         i++;
     }
     ShowElement(DirectionsGrid);
 }