async public Task <MapPolyline> GetRouteAsync(GeoCoordinate from, GeoCoordinate to) { TaskCompletionSource <MapPolyline> tcs = new TaskCompletionSource <MapPolyline>(); RouteServiceClient routeService = new RouteServiceClient("BasicHttpBinding_IRouteService"); routeService.CalculateRouteCompleted += (sender, e) => { var points = e.Result.Result.RoutePath.Points; var coordinates = points.Select(x => new GeoCoordinate(x.Latitude, x.Longitude)); var routeColor = Colors.Blue; var routeBrush = new SolidColorBrush(routeColor); var routeLine = new MapPolyline() { Locations = new LocationCollection(), Stroke = routeBrush, Opacity = 0.65, StrokeThickness = 5.0, }; foreach (var location in points) { routeLine.Locations.Add(new GeoCoordinate(location.Latitude, location.Longitude)); } tcs.TrySetResult(routeLine); }; routeService.CalculateRouteAsync(new RouteRequest() { Credentials = new RoutesAPI.Credentials() { ApplicationId = Settings.BingmapsAPIKey }, Options = new RouteOptions() { RoutePathType = RoutePathType.Points }, Waypoints = new ObservableCollection <Waypoint>( new Waypoint[] { new Waypoint { Location = new Location { Latitude = to.Latitude, Longitude = to.Longitude } }, new Waypoint { Location = new Location { Latitude = from.Latitude, Longitude = from.Longitude } } }) }); return(await tcs.Task); }
public RouteDrawing() { InitializeComponent(); List<GeoCoordinate> locations = new List<GeoCoordinate>(); RouteServiceClient routeService = new RouteServiceClient("BasicHttpBinding_IRouteService"); routeService.CalculateRouteCompleted += (sender, e) => { var points = e.Result.Result.RoutePath.Points; var coordinates = points.Select(x => new GeoCoordinate(x.Latitude, x.Longitude)); var routeColor = Colors.Blue; var routeBrush = new SolidColorBrush(routeColor); var routeLine = new MapPolyline() { Locations = new LocationCollection(), Stroke = routeBrush, Opacity = 0.65, StrokeThickness = 5.0, }; foreach (var location in points) { routeLine.Locations.Add(new GeoCoordinate(location.Latitude, location.Longitude)); } RouteLayer.Children.Add(routeLine); }; RouteBingMap.SetView(LocationRect.CreateLocationRect(locations)); routeService.CalculateRouteAsync(new RouteRequest() { Credentials = new Credentials() { ApplicationId = "Ak-j1fNexs-uNWG_YoP2WZlthezPoUWsRvSexDLTGfjQ1XqKgnfR1nqeC2YbZZSn" }, Options = new RouteOptions() { RoutePathType = RoutePathType.Points }, Waypoints = new ObservableCollection<Waypoint>( locations.Select(x => new Waypoint() { Location = x.Location })) }); }
/// <summary> /// Calculates a route, based on geocode resuls. /// </summary> private void CalculateRoute(GeocodeResult[] locations = null) { // Preparing a request for route calculation. var request = new RouteRequest() { Culture = CultureInfo.CurrentUICulture.Name, Waypoints = new ObservableCollection <Waypoint>(), // Don't raise exceptions. ExecutionOptions = new UsingBingMaps.Bing.Route.ExecutionOptions() { SuppressFaults = true }, // Only accept results with high confidence. Options = new RouteOptions() { RoutePathType = RoutePathType.Points } }; if (from_address == false) { Waypoint w_to = new Waypoint(); Waypoint w_from = new Waypoint(); w_to.Location = x_to; w_from.Location = x_from; request.Waypoints.Add(w_from); request.Waypoints.Add(w_to); } else { foreach (var result in locations) { request.Waypoints.Add(GeocodeResultToWaypoint(result)); } } // Get credentials and only then place an async call on the route 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. _routeClient.CalculateRouteAsync(request); }); }
void pin_Tap(object sender1, System.Windows.Input.GestureEventArgs e1) { List <GeoCoordinate> locations = new List <GeoCoordinate>(); locations.Add(currentLocation); Pushpin senderAs = sender1 as Pushpin; locations.Add(senderAs.Location); destination = senderAs; DestinationPoint.Children.Clear(); DestinationPoint.Children.Add(destination); RouteServiceClient routeService = new RouteServiceClient("BasicHttpBinding_IRouteService"); routeService.CalculateRouteCompleted += (sender, e) => { DrawRoute(e); }; mapBing.SetView(LocationRect.CreateLocationRect(locations)); routeService.CalculateRouteAsync(new RouteRequest() { Credentials = new Credentials() { ApplicationId = LocationManager.bingApiKey }, Options = new RouteOptions() { RoutePathType = RoutePathType.Points }, Waypoints = new ObservableCollection <Waypoint>( locations.Select(x => new Waypoint() { Location = new Microsoft.Phone.Controls.Maps.Platform.Location() { Latitude = x.Latitude, Longitude = x.Longitude } })) }); SetDriveMode(true); }
private void CalculateRoute() { RouteServiceClient routeService = new RouteServiceClient("BasicHttpBinding_IRouteService"); routeService.CalculateRouteCompleted += new EventHandler <CalculateRouteCompletedEventArgs>(routeService_CalculateRouteCompleted); RouteRequest request = new RouteRequest(); request.Credentials = new Credentials(); request.Credentials.ApplicationId = ((ApplicationIdCredentialsProvider)map.CredentialsProvider).ApplicationId; request.Options = new RouteOptions(); request.Options.Optimization = RouteOptimization.MinimizeTime; request.Options.RoutePathType = RoutePathType.Points; request.Waypoints = new System.Collections.ObjectModel.ObservableCollection <Waypoint>(); Waypoint wayPointAcquirer = new Waypoint(); wayPointAcquirer.Location = new Location(); wayPointAcquirer.Location.Latitude = 40.738965; wayPointAcquirer.Location.Longitude = -73.996047; request.Waypoints.Add(wayPointAcquirer); foreach (UserModel user in list_User_Choosed) { Waypoint wayPoint = new Waypoint(); wayPoint.Description = user.Address; wayPoint.Location = new Location(); wayPoint.Location.Latitude = user.Latitude; wayPoint.Location.Longitude = user.Longitude; request.Waypoints.Add(wayPoint); } //foreach (GeocodeResult result in results) //{ // Waypoint wayPoint = new Waypoint(); // wayPoint.Description = result.DisplayName; // wayPoint.Location = new Location(); // wayPoint.Location.Latitude = result.Locations[0].Latitude; // wayPoint.Location.Longitude = result.Locations[0].Longitude; // request.Waypoints.Add(wayPoint); //} routeService.CalculateRouteAsync(request); }
private void CalculateRouting(GeoCoordinate userPosition) { Waypoint endPosition = new Waypoint(); endPosition.Location = userPosition; var routeRequest = new RouteRequest(); routeRequest.Credentials = new Credentials(); routeRequest.Credentials.ApplicationId = "Al9X2Bk2UP07iOZG9N_pt4yGUWpLHcyGmG5EjiRSZDFi4EJn6AnF6MxtGfehDJZi"; routeRequest.Waypoints = new System.Collections.ObjectModel.ObservableCollection<Waypoint>(); routeRequest.Waypoints.Add(StartPosition); routeRequest.Waypoints.Add(endPosition); routeRequest.Options = new RouteOptions(); routeRequest.Options.RoutePathType = RoutePathType.Points; routeRequest.UserProfile = new UserProfile(); routeRequest.UserProfile.DistanceUnit = DistanceUnit.Kilometer; var routeClient = new RouteServiceClient("BasicHttpBinding_IRouteService"); routeClient.CalculateRouteCompleted += new EventHandler<CalculateRouteCompletedEventArgs>(routeClient_CalculateRouteCompleted); routeClient.CalculateRouteAsync(routeRequest); }
private void CalculateRoute() { RouteServiceClient routeService = new RouteServiceClient("BasicHttpBinding_IRouteService"); routeService.CalculateRouteCompleted += new EventHandler<CalculateRouteCompletedEventArgs>(routeService_CalculateRouteCompleted); RouteRequest request = new RouteRequest(); request.Credentials = new Credentials(); request.Credentials.ApplicationId = ((ApplicationIdCredentialsProvider)map.CredentialsProvider).ApplicationId; request.Options = new RouteOptions(); request.Options.Optimization = RouteOptimization.MinimizeTime; request.Options.RoutePathType = RoutePathType.Points; request.Waypoints = new System.Collections.ObjectModel.ObservableCollection<Waypoint>(); Waypoint wayPointAcquirer = new Waypoint(); wayPointAcquirer.Location = new Location(); wayPointAcquirer.Location.Latitude = 40.738965; wayPointAcquirer.Location.Longitude = -73.996047; request.Waypoints.Add(wayPointAcquirer); foreach (UserModel user in list_User_Choosed) { Waypoint wayPoint = new Waypoint(); wayPoint.Description = user.Address; wayPoint.Location = new Location(); wayPoint.Location.Latitude = user.Latitude; wayPoint.Location.Longitude = user.Longitude; request.Waypoints.Add(wayPoint); } //foreach (GeocodeResult result in results) //{ // Waypoint wayPoint = new Waypoint(); // wayPoint.Description = result.DisplayName; // wayPoint.Location = new Location(); // wayPoint.Location.Latitude = result.Locations[0].Latitude; // wayPoint.Location.Longitude = result.Locations[0].Longitude; // request.Waypoints.Add(wayPoint); //} routeService.CalculateRouteAsync(request); }
public void GetDirections(List<Point> locations, EventHandler onResults) { if (IsInitialized) { var routeRequest = new RouteRequest {Credentials = new Credentials {Token = token}}; var waypoints = new List<Waypoint>(); foreach (Point loc in locations) { waypoints.Add(new Waypoint {Location = new Location {Longitude = loc.X, Latitude = loc.Y}}); } routeRequest.Waypoints = waypoints; routeRequest.Options = new RouteOptions { Mode = TravelMode, Optimization = RouteOptimization, RoutePathType = RoutePathType.Points, TrafficUsage = TrafficUsage }; var routeService = new RouteServiceClient(); routeService.CalculateRouteCompleted += (o, e) => { RouteResponse routeResponse = e.Result; onResults(this, new RouteResultArgs {Result = routeResponse.Result}); }; routeService.CalculateRouteAsync(routeRequest); } }
void pin_Tap(object sender1, System.Windows.Input.GestureEventArgs e1) { List<GeoCoordinate> locations = new List<GeoCoordinate>(); locations.Add(currentLocation); Pushpin senderAs = sender1 as Pushpin; locations.Add(senderAs.Location); destination = senderAs; DestinationPoint.Children.Clear(); DestinationPoint.Children.Add(destination); RouteServiceClient routeService = new RouteServiceClient("BasicHttpBinding_IRouteService"); routeService.CalculateRouteCompleted += (sender, e) => { DrawRoute(e); }; mapBing.SetView(LocationRect.CreateLocationRect(locations)); routeService.CalculateRouteAsync(new RouteRequest() { Credentials = new Credentials() { ApplicationId = LocationManager.bingApiKey }, Options = new RouteOptions() { RoutePathType = RoutePathType.Points }, Waypoints = new ObservableCollection<Waypoint>( locations.Select(x => new Waypoint() { Location = new Microsoft.Phone.Controls.Maps.Platform.Location() { Latitude = x.Latitude, Longitude = x.Longitude } })) }); SetDriveMode(true); }
public void RouteCalc(Pushpin p) { // Create the service variable and set the callback method using the CalculateRouteCompleted property. RouteServiceClient routeService = new RouteServiceClient("BasicHttpBinding_IRouteService"); routeService.CalculateRouteCompleted += new EventHandler<CalculateRouteCompletedEventArgs>(routeService_CalculateRouteCompleted); // Set the token. RouteRequest routeRequest = new RouteRequest(); routeRequest.Credentials = new LimaRoute.RouteService.Credentials(); routeRequest.Credentials.ApplicationId = "Av7R1AqbtqdVUDABzrWemEJHTh4ddfpZe6J_Y--onTh1xJ_a_hualfbL08DKiOB5"; // Return the route points so the route can be drawn. routeRequest.Options = new RouteOptions(); routeRequest.Options.RoutePathType = RoutePathType.Points; // Set the waypoints of the route to be calculated using the Geocode Service results stored in the geocodeResults variable. Waypoint waypoint1 = new Waypoint(); waypoint1.Location = new Location(); waypoint1.Description = "Mi ubicación"; waypoint1.Location.Latitude = watcher.Position.Location.Latitude; waypoint1.Location.Longitude = watcher.Position.Location.Longitude; Waypoint waypoint2 = new Waypoint(); waypoint2.Location = new Location(); waypoint2.Description = "Destino"; waypoint2.Location.Latitude = p.Location.Latitude; waypoint2.Location.Longitude = p.Location.Longitude; //Add the waypoints routeRequest.Waypoints = new ObservableCollection<Waypoint>(); routeRequest.Waypoints.Add(waypoint1); routeRequest.Waypoints.Add(waypoint2); // Make the CalculateRoute asnychronous request. routeService.CalculateRouteAsync(routeRequest); }
// This method makes the initial CalculateRoute asynchronous request using the results of the Geocode Service. private void CalculateRoute(GeocodeService.GeocodeResult[] locations) { // Create the service variable and set the callback method using the CalculateRouteCompleted property. RouteServiceClient routeService = new RouteServiceClient("BasicHttpBinding_IRouteService"); routeService.CalculateRouteCompleted += new EventHandler<CalculateRouteCompletedEventArgs>(routeService_CalculateRouteCompleted); // Set the credentials. RouteService.RouteRequest routeRequest = new RouteService.RouteRequest(); routeRequest.Culture = MyMap.Culture; routeRequest.Credentials = new RouteService.Credentials(); routeRequest.Credentials.ApplicationId = ((ApplicationIdCredentialsProvider)MyMap.CredentialsProvider).ApplicationId; // 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 locations) { routeRequest.Waypoints.Add(GeocodeResultToWaypoint(result)); } // Make asynchronous call to fetch the data ... pass state object. // Make the CalculateRoute asnychronous request. routeService.CalculateRouteAsync(routeRequest); }
public void GetTripAsync(List<Meeting> meetings) { if (meetings.Count()<=1) return; //Initialisation var request = new RouteRequest(); request.Credentials = new Credentials(); request.Credentials.ApplicationId = BingMapCredential.CREDENTIAL; request.Waypoints = new ObservableCollection<Waypoint>(); foreach (Meeting meeting in meetings) { if (IsCanceled) return; if (meeting.IsLocationFail) continue; request.Waypoints.Add(new Waypoint() { Location = new Location() { Latitude = meeting.Location.Latitude, Longitude = meeting.Location.Longitude } }); } request.Options = new RouteOptions(); request.Options.RoutePathType = RoutePathType.Points; request.UserProfile = new UserProfile(); request.UserProfile.DistanceUnit = DistanceUnit.Kilometer; var service = new RouteServiceClient("BasicHttpBinding_IRouteService"); //Réponse service.CalculateRouteCompleted += (o, e) => { if (IsCanceled) return; if (e.Error != null || e.Result == null) { try { Debug.WriteLine(e.Error.Message); } catch (Exception exception) { } TripReceived(this, new TripReceivedEventArgs(){Error = true}); return; } List<Trip> trips = new List<Trip>(); var legs = e.Result.Result.Legs; var countRoutePath = e.Result.Result.RoutePath.Points.Count; int lastIndex = 0; var count = legs.Count; for (int i = 0; i < count; i++) { if (IsCanceled) return; var leg = legs[i]; Trip trip = new Trip(); meetings[i].Location = leg.ActualStart; meetings[i + 1].Location = leg.ActualEnd; trip.Start = meetings[i]; trip.End = meetings[i + 1]; trip.Duration = leg.Summary.TimeInSeconds / 60; trip.Distance = leg.Summary.Distance; while (lastIndex < countRoutePath) { if (IsCanceled) return; Location point = e.Result.Result.RoutePath.Points[lastIndex]; if (Math.Round(trip.End.Location.Latitude, 5) == Math.Round(point.Latitude, 5) && Math.Round(trip.End.Location.Longitude, 5) == Math.Round(point.Longitude, 5)) { break; } trip.RoutePath.Add(point); lastIndex++; } trips.Add(trip); } TripReceived(this, new TripReceivedEventArgs(trips, e.Result.Result) ); }; //Appel service.CalculateRouteAsync(request); if (IsCanceled) return; }