Internally used for passing state between route asynchronous calls.
        private void client_GeocodeCompleted(object sender, GeocodeCompletedEventArgs e)
        {
            try
            {
                RoutingState  state  = e.UserState as RoutingState;
                GeocodeResult result = null;

                lock (StateSync)
                {
                    if (_geoFailed)
                    {
                        return;
                    }
                }

                if (e.Result.ResponseSummary.StatusCode != Bing.Geocode.ResponseStatusCode.Success ||
                    e.Result.Results.Count == 0)
                {
                    lock (StateSync)
                    {
                        _geoFailed = true;
                    }

                    // Report geocode error.
                    _uiDispatcher.BeginInvoke(() => Error(new RouteCalculationError(e)));

                    return;
                }

                bool doneGeocoding = false;

                lock (StateSync)
                {
                    // Only report on first result.
                    result = e.Result.Results.First();

                    // Update state object ... when all the results are set, call route.
                    state.Results[state.LocationNumber] = result;
                    doneGeocoding = state.GeocodesComplete;
                }

                if (doneGeocoding && state.GeocodesSuccessful)
                {
                    // Calculate the route.
                    CalculateRoute(state.Results);
                }
            }
            catch (Exception ex)
            {
                lock (StateSync)
                {
                    _geoFailed = true;
                }

                _uiDispatcher.BeginInvoke(() => Error(new RouteCalculationError(ex.Message, ex)));
            }
        }
Пример #2
0
        public void CalculateAsync()
        {
            // Geocode locations in parallel.
            var results = new GeocodeResult[2];

            // To location.
            var state1 = new RoutingState(results, 1, _to);

            GeocodeAddress(_to, state1);

            // From location.
            var state0 = new RoutingState(results, 0, _from);

            GeocodeAddress(_from, state0);
        }
        private void GeocodeAddress(string address, RoutingState state)
        {
            var request = new GeocodeRequest()
            {
                Culture = CultureInfo.CurrentUICulture.Name,
                Query   = address,

                // Don't raise exceptions.
                ExecutionOptions = new UsingBingMaps.Bing.Geocode.ExecutionOptions()
                {
                    SuppressFaults = true
                },

                // Only accept results with high confidence.
                Options = new GeocodeOptions()
                {
                    Filters = new ObservableCollection <FilterBase>
                    {
                        new ConfidenceFilter()
                        {
                            MinimumConfidence = UsingBingMaps.Bing.Geocode.Confidence.High
                        }
                    }
                }
            };

            // Get credentials and only then place an async call on the geocode service.
            _credentialsProvider.GetCredentials(credentials =>
            {
                // Pass in credentials for web services call.
                // Replace with your own Credentials.
                request.Credentials = credentials;

                // Make asynchronous call to fetch the data.
                _geocodeClient.GeocodeAsync(request, state);
            });
        }
        private void GeocodeAddress(string address, RoutingState state)
        {
            var request = new GeocodeRequest()
            {
                Culture = CultureInfo.CurrentUICulture.Name,
                Query = address,

                // Don't raise exceptions.
                ExecutionOptions = new UsingBingMaps.Bing.Geocode.ExecutionOptions()
                {
                    SuppressFaults = true
                },

                // Only accept results with high confidence.
                Options = new GeocodeOptions()
                {
                    Filters = new ObservableCollection<FilterBase>
                    {
                        new ConfidenceFilter()
                        {
                            MinimumConfidence = UsingBingMaps.Bing.Geocode.Confidence.High
                        }
                    }
                }
            };

            // Get credentials and only then place an async call on the geocode service.
            _credentialsProvider.GetCredentials(credentials =>
            {
                // Pass in credentials for web services call.
                // Replace with your own Credentials.
                request.Credentials = credentials;

                // Make asynchronous call to fetch the data.
                _geocodeClient.GeocodeAsync(request, state);
            });
        }
        public void CalculateAsync()
        {
            if (from_address == true)
            {
                // Geocode locations in parallel.
                var results = new GeocodeResult[2];

                // To location.
                var state1 = new RoutingState(results, 1, _to);
                GeocodeAddress(_to, state1);

                // From location.
                var state0 = new RoutingState(results, 0, _from);
                GeocodeAddress(_from, state0);
            }
        }