private async void BuildTheRoute_Click(object sender, RoutedEventArgs e) { if (_mapPoints.Count < 2) { return; } CheckRouteServicePreffer(); if (_mapPoints.Count > 47 && !_manualRoute) { MessageBox.Show( "Too many waypoints, try to reduce them to 47, or wait for next releases, where that limit will be increased!", "Routing Error", MessageBoxButton.OK, MessageBoxImage.Information); return; } BuildingProgressBar.Value = 0; BotWindowData bot; var route = GetWorkingRouting(out bot); if (route == "error" && !_manualRoute) { MessageBox.Show( "You have to enter Google Direction API or Mapzen Valhalla API to any of your bots, before creating a route", "API Key Error", MessageBoxButton.OK, MessageBoxImage.Information); return; } var start = _mapPoints.FirstOrDefault(x => x.IsStart) ?? _mapPoints.First(); BuildingProgressBar.Value = 10; RoutingResponse response = null; var cycleWp = _mapPoints.Where(x => !x.IsStart).Select(x => x.Location).ToList(); cycleWp.Add(start.Location); List <GeoCoordinate> routePoints; if (!_manualRoute) { if (route == "google") { response = GoogleRouting.GetRoute(start.Location, null, bot.Session, cycleWp, true); } else if (route == "mapzen") { response = MapzenRouting.GetRoute(start.Location, null, bot.Session, cycleWp, true); } if (response?.Coordinates == null || response.Coordinates.Count == 0) { return; } routePoints = response.Coordinates.Select(wp => new GeoCoordinate(wp[0], wp[1])).ToList(); } else { cycleWp.Insert(0, start.Location); routePoints = new List <GeoCoordinate>(cycleWp); } BuildingProgressBar.Value = 60; _currentRoute?.Points?.Clear(); if (_currentRoute == null) { _currentRoute = new GMapRoute(new List <PointLatLng>()); RouteCreatorMap.Markers.Add(_currentRoute); } BuildingProgressBar.Value = 70; _buildedRoute = new List <GeoCoordinate>(routePoints); foreach (var item in routePoints) { _currentRoute.Points?.Add(new PointLatLng(item.Latitude, item.Longitude)); } _currentRoute?.RegenerateShape(RouteCreatorMap); var path = _currentRoute?.Shape as Path; if (path != null) { path.Stroke = new SolidColorBrush(Color.FromRgb(255, 0, 0)); } bot = MainWindow.BotsCollection.FirstOrDefault( x => !string.IsNullOrEmpty(x.GlobalSettings.LocationSettings.MapzenApiElevationKey)); if (bot != null) { await bot.Session.MapzenApi.FillAltitude(_buildedRoute.ToList()); } BuildingProgressBar.Value = 100; _builded = true; }
private async Task BuildTheRouteTask(CancellationToken token) { try { token.ThrowIfCancellationRequested(); if (_mapPoints.Count < 2) { return; } await Dispatcher.BeginInvoke(new ThreadStart(CheckRouteServicePrefer)); if (_mapPoints.Count > 47 && !_manualRoute) { Dispatcher.Invoke(new ThreadStart(delegate { MessageBox.Show( TranslationEngine.GetDynamicTranslationString("%TOO_MANY_ROUTE_POINTS%", "Too many waypoints, try to reduce them to 47, or wait for next releases, where that limit will be increased!"), "Routing Error", MessageBoxButton.OK, MessageBoxImage.Information); })); return; } UpdateProgress(TranslationEngine.GetDynamicTranslationString("%ROUTE_PROGRESS_2%", "Started!..."), 0); BotWindowData bot; var route = GetWorkingRouting(out bot); if (route == "error" && !_manualRoute) { MessageBox.Show( TranslationEngine.GetDynamicTranslationString("%NO_ROUTE_API_FOUND%", "You have to enter Google Direction API or Mapzen Valhalla API to any of your bots, before creating a route"), "API Key Error", MessageBoxButton.OK, MessageBoxImage.Information); return; } var start = _mapPoints.FirstOrDefault(x => x.IsStart) ?? _mapPoints.First(); UpdateProgress(TranslationEngine.GetDynamicTranslationString("%ROUTE_PROGRESS_3%", "Started!..."), 10); RoutingResponse response = null; var cycleWp = _mapPoints.Where(x => !x.IsStart).Select(x => x.Location).ToList(); cycleWp.Add(start.Location); List <GeoCoordinate> routePoints; token.ThrowIfCancellationRequested(); if (!_manualRoute) { if (route == "google") { response = GoogleRouting.GetRoute(start.Location, null, bot.Session, cycleWp, true); } else if (route == "mapzen") { response = MapzenRouting.GetRoute(start.Location, null, bot.Session, cycleWp, true); } if (response?.Coordinates == null || response.Coordinates.Count == 0) { return; } routePoints = response.Coordinates.Select(wp => new GeoCoordinate(wp[0], wp[1])).ToList(); } else { cycleWp.Insert(0, start.Location); routePoints = new List <GeoCoordinate>(cycleWp); } token.ThrowIfCancellationRequested(); UpdateProgress( TranslationEngine.GetDynamicTranslationString("%ROUTE_PROGRESS_4%", "Handling result..."), 60); _currentRoute?.Points?.Clear(); if (_currentRoute == null) { _currentRoute = new GMapRoute(new List <PointLatLng>()); } await RouteCreatorMap.Dispatcher.BeginInvoke(new ThreadStart(delegate { RouteCreatorMap.Markers.Add(_currentRoute); })); token.ThrowIfCancellationRequested(); UpdateProgress( TranslationEngine.GetDynamicTranslationString("%ROUTE_PROGRESS_5%", "Requesting altitude..."), 70); _buildedRoute = new List <GeoCoordinate>(routePoints); token.ThrowIfCancellationRequested(); foreach (var item in routePoints) { _currentRoute.Points?.Add(new PointLatLng(item.Latitude, item.Longitude)); } await Dispatcher.BeginInvoke(new ThreadStart(delegate { try { _currentRoute.RegenerateShape(RouteCreatorMap); } catch (Exception) { //ignore } })); var path = _currentRoute?.Shape as Path; await Dispatcher.BeginInvoke(new ThreadStart(delegate { if (path != null) { path.Stroke = new SolidColorBrush(Color.FromRgb(255, 0, 0)); } })); bot = MainWindow.BotsCollection.FirstOrDefault( x => !string.IsNullOrEmpty(x.GlobalSettings.LocationSettings.MapzenApiKey)); if (bot != null) { await bot.Session.MapzenApi.FillAltitude(_buildedRoute.ToList(), token : token); } UpdateProgress(TranslationEngine.GetDynamicTranslationString("%ROUTE_PROGRESS_6%", "Done!"), 100); _builded = true; } catch (OperationCanceledException) { //ignore } }