/// <summary> /// Perform routing using the RoutingCloudClient through a preset set of waypoints /// </summary> private async void RouteWaypoints() { // Create a set of preset waypoints to route through PointShape startPoint = new PointShape(-10776986.85, 3908680.24); PointShape waypoint1 = new PointShape(-10776836.12, 3912348.04); PointShape waypoint2 = new PointShape(-10778917.01, 3909965.17); PointShape endPoint = new PointShape(-10779631.80, 3915721.82); // Show a loading graphic to let users know the request is running loadingIndicator.IsRunning = true; loadingLayout.IsVisible = true; // Send the routing request CloudRoutingGetRouteResult routingResult = await GetRoute(new Collection <PointShape> { startPoint, waypoint1, waypoint2, endPoint }); // Hide the loading graphic loadingIndicator.IsRunning = false; loadingLayout.IsVisible = false; // Handle an exception returned from the service if (routingResult.Exception != null) { await DisplayAlert("Error", routingResult.Exception.Message, "OK"); return; } //Draw the result on the map DrawRoute(routingResult); }
/// <summary> /// Set options and perform routing using the RoutingCloudClient through a preset set of waypoints /// </summary> private async Task <CloudRoutingGetRouteResult> GetRoute(Collection <PointShape> waypoints) { // Set up options for the routing request // Enable turn-by-turn so we get turn by turn instructions CloudRoutingGetRouteOptions options = new CloudRoutingGetRouteOptions(); options.TurnByTurn = true; CloudRoutingGetRouteResult route = await routingCloudClient.GetRouteAsync(waypoints, 3857, options); return(route); }
/// <summary> /// Draw the result of a Cloud Routing request on the map /// </summary> private void DrawRoute(CloudRoutingGetRouteResult routingResult) { // Get the routing feature layer from the MapView InMemoryFeatureLayer routingLayer = (InMemoryFeatureLayer)mapView.FindFeatureLayer("Routing Layer"); // Clear the previous features from the routing layer routingLayer.InternalFeatures.Clear(); // Create a collection to hold the route segments. These include information like distance, duration, warnings, and instructions for turn-by-turn routing List <CloudRoutingSegment> routeSegments = new List <CloudRoutingSegment>(); int index = 0; // Add the route waypoints and route segments to the map foreach (CloudRoutingWaypoint waypoint in routingResult.RouteResult.Waypoints) { Dictionary <string, string> columnValues = new Dictionary <string, string>(); // Get the order of the stops and label the point // '0' represents the start/end point of the route for a round trip route, so we change the label to indicate that for readability columnValues.Add("SequenceNumber", (index == 0 ? "Start Point" : "Stop " + index)); PointShape routeWaypoint = new PointShape(waypoint.Coordinate); // Add the point to the map routingLayer.InternalFeatures.Add(new Feature(routeWaypoint, columnValues)); // Increment the index for labeling purposes index++; } foreach (CloudRoutingRoute route in routingResult.RouteResult.Routes) { routingLayer.InternalFeatures.Add(new Feature(route.Shape)); routeSegments.AddRange(route.Segments); } // Set the data source for the list box to the route segments lsbRouteSegments.ItemsSource = routeSegments; // Set the map extent to the newly displayed route routingLayer.Open(); mapView.CurrentExtent = RectangleShape.ScaleUp(routingLayer.GetBoundingBox(), 20).GetBoundingBox(); routingLayer.Close(); mapView.Refresh(); }