// This is the Geocode request callback method. private void geocodeService_GeocodeCompleted(object sender, geocodeservice.GeocodeCompletedEventArgs e) { // Retrieve the user state of this response (the ‘waypoint index’) to identify which geocode request // it corresponds to. int waypointIndex = System.Convert.ToInt32(e.UserState); // Retrieve the GeocodeResult for this response and store it in the global variable geocodeResults, using // the waypoint index to position it in the array. geocodeResults[waypointIndex] = e.Result.Results[0]; // Look at each element in the global gecodeResults array to figure out if more geocode responses still // need to be returned. bool doneGeocoding = true; foreach (geocodeservice.GeocodeResult gr in geocodeResults) { if (gr == null) { doneGeocoding = false; } } // If the geocodeResults array is totally filled, then calculate the route. if (doneGeocoding) CalculateRoute(geocodeResults); }
private void CalculateRoute(geocodeservice.GeocodeResult[] results) { // Create the service variable and set the callback method using the CalculateRouteCompleted property. routeservice.RouteServiceClient routeService = new routeservice.RouteServiceClient("BasicHttpBinding_IRouteService"); routeService.CalculateRouteCompleted += new EventHandler<routeservice.CalculateRouteCompletedEventArgs>(routeService_CalculateRouteCompleted); // Set the token. routeservice.RouteRequest routeRequest = new routeservice.RouteRequest(); routeRequest.Credentials = new Credentials(); routeRequest.Credentials.ApplicationId = "ApgLkoHIG4rNShRJAxMMNettsv6SWs3eP8OchozFS89Vex7BRHsSbCr31HkvYK-d"; // Return the route points so the route can be drawn. routeRequest.Options = new routeservice.RouteOptions(); routeRequest.Options.RoutePathType = routeservice.RoutePathType.Points; // Set the waypoints of the route to be calculated using the Geocode Service results stored in the geocodeResults variable. routeRequest.Waypoints = new System.Collections.ObjectModel.ObservableCollection<routeservice.Waypoint>(); foreach (geocodeservice.GeocodeResult result in results) { routeRequest.Waypoints.Add(GeocodeResultToWaypoint(result)); } // Make the CalculateRoute asnychronous request. routeService.CalculateRouteAsync(routeRequest); }
private routeservice.Waypoint GeocodeResultToWaypoint(geocodeservice.GeocodeResult result) { routeservice.Waypoint waypoint = new routeservice.Waypoint(); waypoint.Description = result.DisplayName; waypoint.Location = new Location(); waypoint.Location.Latitude = result.Locations[0].Latitude; waypoint.Location.Longitude = result.Locations[0].Longitude; return waypoint; }