示例#1
0
        private async void GeocodeButton_Click(object sender, RoutedEventArgs e)
        {
            GeocodeButton.IsEnabled = false;
            geocodeText.IsEnabled   = false;
            try
            {
                GeoCodeProgress.Visibility = Visibility.Visible;

                if (_stopsOverlay.Graphics.Count() > 0)
                {
                    panelResults.Visibility = Visibility.Collapsed;
                    _stopsOverlay.Graphics.Clear();
                    _routesOverlay.Graphics.Clear();
                    MyMapView.Overlays.Items.Clear();
                    _directionsOverlay.GraphicsSource = null;
                }

                if (_locatorServiceInfo == null)
                {
                    _locatorServiceInfo = await _locatorTask.GetInfoAsync();
                }

                var candidateResults = await _locatorTask.GeocodeAsync(
                    GetInputAddressFromUI(), new List <string> {
                    "Addr_type", "Score", "X", "Y"
                }, MyMapView.SpatialReference, CancellationToken.None);

                if (candidateResults == null || candidateResults.Count == 0)
                {
                    throw new Exception("No se encontró la dirección.");
                }

                var found = false;
                foreach (var candidate in candidateResults)
                {
                    if (candidate.Score >= 90)
                    {
                        AddGraphicFromLocatorCandidate(candidate);
                        found = true;
                        MyMapView.SetView(candidate.Extent.Expand(1));
                        await MyMapView.ZoomToScaleAsync(5000);

                        break;
                    }
                }
                GeocodeFlyout.Hide();
                if (!found)
                {
                    throw new Exception("No se encontró la dirección.");
                }
            }
            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()), "Error").ShowAsync();
                }
                else
                {
                    var _x = new MessageDialog(ex.Message, "Error").ShowAsync();
                }
            }
            catch (System.Exception ex)
            {
                var _x = new MessageDialog(ex.Message, "Error").ShowAsync();
            }
            finally
            {
                GeoCodeProgress.Visibility = Visibility.Collapsed;
                GeocodeButton.IsEnabled    = true;
                geocodeText.IsEnabled      = true;
            }
        }