/// <summary> /// Finish initializations after setting the viewmodel /// </summary> /// <returns></returns> private async Task InitializeFromViewModel() { this.DataContext = this.ViewModel; ProgressIndicatorHelper.Instance.Push(LoadingEnum.Stops); //PIHelper.Push("Getting stops..."); //var ls = await TransitInfo.GetStopsOfRoute(this.ViewModel.Context); if (!ViewModel.Context.Stop_Ids.Any(id => !AppSettings.KnownStops.Value.ContainsKey(id)) || TransitLoader.InternetAvailable()) { await ViewModel.GetStopsOfRoute(); } ProgressIndicatorHelper.Instance.Remove(LoadingEnum.Stops); // Center the map if (this.ViewModel.Stops.Count > 0) { this.MapItems.ItemsSource = this.ViewModel.Stops; this.startup = false; this.ResultsMap.SetView(LocationRectangle.CreateBoundingRectangle(this.ViewModel.Stops.Select(s => s.Location))); } else { this.startup = false; } }
/// <summary> /// Get all stops that the route visits /// </summary> /// <returns></returns> public async Task GetStopsOfRoute() { List <BusStop> stopList = new List <BusStop>(); if (this.Context == null) { return; } if (this.Context.Stop_Ids == null) { return; } NumStopsVisibility = Visibility.Visible; NumStops = 0; foreach (string id in this.Context.Stop_Ids) { BusStop stop; if (AppSettings.KnownStops.Value.ContainsKey(id)) { stop = AppSettings.KnownStops.Value[id]; } else { stop = await TransitLoader.GetStop(id); if (stop == null) { break; } await TransitLoader.GetTransitNetwork(stop.Location); } stopList.Add(stop); NumStops++; } NumStopsVisibility = Visibility.Collapsed; // Update Stops Observable Collection if (LocationTracker.Location != null) { stopList = stopList.OrderBy(s => LocationTracker.Location.GetDistanceTo(s.Location)).ToList(); } int numbering = 0; foreach (BusStop bs in stopList) { numbering++; bs.Number = numbering; this.Stops.Add(bs); } }
/// <summary> /// Finds the nearest matching route /// </summary> /// <param name="routeName">Name of route</param> /// <returns></returns> private static async Task <BusRoute> GetNearestMatchingRoute(string routeName) { BusRoute retRoute = null; if (RouteIDs.ContainsKey(routeName)) { if (RouteIDs[routeName].Count == 0) { return(null); } if (RouteIDs[routeName].Count == 1) { if (!AppSettings.KnownRoutes.Value.ContainsKey(RouteIDs[routeName][0])) { return(null); } retRoute = await TransitLoader.GetRoute(RouteIDs[routeName][0]); return(retRoute); } double minDist = double.PositiveInfinity; foreach (string id in RouteIDs[routeName]) { if (!AppSettings.KnownRoutes.Value.ContainsKey(id)) { return(retRoute); } var br = AppSettings.KnownRoutes.Value[id]; if (br.Stop_Ids == null || br.Stop_Ids.Count == 0) { return(retRoute); } BusStop firststop = await TransitLoader.GetStop(br.Stop_Ids[0]); if (firststop == null) { continue; } var dist = MyLocation.GetDistanceTo(firststop.Location); if (minDist > dist) { retRoute = br; minDist = dist; } } return(retRoute); } return(null); }
/// <summary> /// Search the text in the route box /// </summary> /// <returns></returns> private async Task SearchRoutes() { this.Focus(); // Blank route if (this.RouteSearchBox.Text == EmptyTextBox || string.IsNullOrWhiteSpace(this.RouteSearchBox.Text)) { MessageBox.Show("Please enter the number of a bus route in your area.", "No input", MessageBoxButton.OK); return; } ProgressIndicatorHelper.Instance.Push(LoadingEnum.Routes); BusRoute br = await TransitInfo.SearchForRoute(this.RouteSearchBox.Text); if (br == null && !AppSettings.LocationConsent.Value && LocationTracker.GetPermission()) { br = await TransitInfo.SearchForRoute(this.RouteSearchBox.Text); } ProgressIndicatorHelper.Instance.Remove(LoadingEnum.Routes); if (br == null) { TransitLoader.InternetAvailable(); MessageBox.Show(string.Format("Could not find route {0}.", this.RouteSearchBox.Text), "No matches", MessageBoxButton.OK); return; } this.RouteSearchBox.Text = ""; if (this.SpacingPanel.Height == spacerMaxHeight) { this.SetSearchBarVisibility(Visibility.Collapsed); } NavigationService.Navigate(new Uri( string.Format("/Pages/StopResultPage.xaml?route_id={0}", br.Id), UriKind.Relative)); //NavigationService.Navigate(new Uri("/Pages/StopResultPage.xaml", UriKind.Relative)); }
/// <summary> /// Search for route within known routes. /// If not found, load up nearby transit info and search again. /// TODO: If there is a known far away stop, this will fail. /// </summary> /// <param name="routeName"></param> /// <returns></returns> public static async Task <BusRoute> SearchForRoute(string routeName) { BusRoute result = null; result = await GetNearestMatchingRoute(routeName); if (result != null) { return(result); } if (MyLocation != null) { await TransitLoader.GetTransitNetwork(TransitInfo.MyLocation); } //RefreshRouteIDs(); await RefreshRouteIDsAsync(); result = await GetNearestMatchingRoute(routeName); return(result); }