Exemplo n.º 1
0
        // Calculate the route
        private async void MyMapView_MapViewDoubleTapped(object sender, Esri.ArcGISRuntime.Controls.MapViewInputEventArgs e)
        {
            if (_stopsOverlay.Graphics.Count() < 2)
            {
                return;
            }

            try
            {
                e.Handled = true;

                panelResults.Visibility = Visibility.Collapsed;
                progress.Visibility     = Visibility.Visible;

                RouteParameters routeParams = await _routeTask.GetDefaultParametersAsync();

                routeParams.OutSpatialReference  = MyMapView.SpatialReference;
                routeParams.ReturnDirections     = true;
                routeParams.DirectionsLengthUnit = LinearUnits.Miles;
                routeParams.DirectionsLanguage   = new CultureInfo("en-Us");               // CultureInfo.CurrentCulture;
                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.25));
            }
            catch (AggregateException ex)
            {
                var message             = ex.Message;
                var innermostExceptions = ex.Flatten().InnerExceptions;
                if (innermostExceptions != null && innermostExceptions.Count > 0)
                {
                    message = innermostExceptions[0].Message;
                }

                var _x = new MessageDialog(message, "Sample Error").ShowAsync();
            }
            catch (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;
                }
            }
        }
        // Geocode input address and add result graphics to the map
        private async void FindButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                progress.Visibility             = Visibility.Visible;
                listResults.Visibility          = Visibility.Collapsed;
                _graphicsOverlay.GraphicsSource = null;

                // Street, City, State, ZIP
                Dictionary <string, string> address = new Dictionary <string, string>();
                if (!string.IsNullOrEmpty(InputAddress.Text))
                {
                    address.Add("Street", InputAddress.Text);
                }
                if (!string.IsNullOrEmpty(City.Text))
                {
                    address.Add("City", City.Text);
                }
                if (!string.IsNullOrEmpty(State.Text))
                {
                    address.Add("State", State.Text);
                }
                if (!string.IsNullOrEmpty(Zip.Text))
                {
                    address.Add("ZIP", Zip.Text);
                }

                if (_locatorTask == null)
                {
                    var path = System.IO.Path.Combine(ApplicationData.Current.LocalFolder.Path, LOCATOR_PATH);
                    _locatorTask = await Task.Run <LocalLocatorTask>(() => new LocalLocatorTask(path));
                }

                var candidateResults = await _locatorTask.GeocodeAsync(
                    address, new List <string> {
                    "Match_addr"
                }, MyMapView.SpatialReference, CancellationToken.None);

                _graphicsOverlay.GraphicsSource = candidateResults
                                                  .Select(result => new Graphic(result.Location, new Dictionary <string, object> {
                    { "Locator", result }
                }));

                await MyMapView.SetViewAsync(ExtentFromGraphics().Expand(2));
            }
            catch (AggregateException ex)
            {
                var innermostExceptions = ex.Flatten().InnerExceptions;
                if (innermostExceptions != null && innermostExceptions.Count > 0)
                {
                    var _x = new MessageDialog(string.Join(" > ", innermostExceptions.Select(i => i.Message).ToArray())).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 (_graphicsOverlay.GraphicsSource != null)
                {
                    listResults.Visibility = Visibility.Visible;
                }
            }
        }