示例#1
0
        private async void MyMap_MouseClick(object sender, ESRI.ArcGIS.Client.Map.MouseEventArgs e)
        {
            try
            {
                if (_cts != null)
                {
                    _cts.Cancel();
                }

                _cts = new CancellationTokenSource();

                Graphic stop = new Graphic()
                {
                    Geometry = e.MapPoint
                };

                _stopsGraphicsLayer.Graphics.Add(stop);

                if (_stopsGraphicsLayer.Graphics.Count > 1)
                {
                    if (_routeTask.IsBusy)
                    {
                        _cts.Cancel();
                        _stopsGraphicsLayer.Graphics.RemoveAt(_stopsGraphicsLayer.Graphics.Count - 1);
                    }

                    SolveRouteResult result = await _routeTask.SolveTaskAsync(_routeParams, _cts.Token);

                    _routeGraphicsLayer.Graphics.Clear();
                    _routeGraphicsLayer.Graphics.Add(result.RouteResults[0].Route);
                }
            }
            catch (Exception ex)
            {
                _stopsGraphicsLayer.Graphics.RemoveAt(_stopsGraphicsLayer.Graphics.Count - 1);

                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 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;
                }
            }
        }