Example #1
0
        public static List <BusStopInfo> GetBusStopArrivalsByPostcode(string postcode, int maxNumberOfStops = -1)
        {
            // Create new list of BusArrivals - returned object
            List <BusStopInfo> bStops = new List <BusStopInfo>();

            //Call PostCodeAPI.RequestPostCodeInfo(postcode)
            PostcodeAPI pcAPI = new PostcodeAPI();

            //Returns postcode object with Long and Lat
            PostcodeInfo pc = pcAPI.RequestPostcodeInfo(postcode);

            //Call BusAPI.RequestStopInfo(lat, long)
            TFLStopPointAPI stopAPI = new TFLStopPointAPI();

            // Returns List of StopPoints(Bus stops)
            List <StopPointInfo> stopsList = stopAPI.RequestStopInfo(pc.Latitude, pc.Longitude);

            // Set number of stops to take
            int stopNo = (maxNumberOfStops <0 || maxNumberOfStops> stopsList.Count) ? stopsList.Count : maxNumberOfStops;

            // For each StopPoint (Limit StopPoints to max number of stops, closest first)
            foreach (StopPointInfo stop in stopsList.OrderBy(s => s.distance).Take(stopNo).ToList())
            {
                // Initialise a new bus stop object
                BusStopInfo bStop = new BusStopInfo()
                {
                    Name     = stop.commonName,
                    Distance = stop.distance,
                    Letter   = stop.stopLetter
                };

                // Call BusAPI.RequestArrivalInfo with StopPoint.Id
                //Returns list of ArrivalInfo(next buses)
                List <ArrivalInfo> arrInfoList = stopAPI.RequestArrivalInfo(stop.naptanId);

                // for each arrival info - first order list by timetoarrival
                foreach (ArrivalInfo arrInfo in arrInfoList.OrderBy(a => a.TimeToStation).ToList())
                {
                    // Create new BusInfo
                    BusInfo bus = new BusInfo()
                    {
                        RouteNumber   = arrInfo.LineName,
                        Destination   = arrInfo.DestinationName,
                        TimeToArrival = arrInfo.TimeToStationInMins
                    };

                    // Add to bus list
                    bStop.Buses.Add(bus);
                }

                // Add completed bus stop to list
                bStops.Add(bStop);
            }

            // return list of bus arrival info
            return(bStops);
        }
Example #2
0
        public List <BusArrivalInfo> GetBusArrivalsByPostcode(string postcode, int maxNumberOfStops)
        {
            //Call PostCodeAPI.RequestPostCodeInfo(postcode)
            PostcodeAPI pcAPI = new PostcodeAPI();

            //Returns postcode object with Long and Lat
            Postcode pc = pcAPI.RequestPostCodeInfo(postcode);

            //Call BusAPI.RequestStopInfo(lat, long)
            TFLBusStopAPI stopAPI = new TFLBusStopAPI();

            // Returns List of StopPoints(Bus stops)
            List <StopPoint> stopsList = stopAPI.RequestStopInfo(pc.Latitude, pc.Longitude);

            // Create new list of BusArrivals
            List <BusArrivalInfo> arrivals = new List <BusArrivalInfo>();

            // For each StopPoint (Limit StopPoints to max number of stops, closest first)
            foreach (StopPoint stop in stopsList.OrderBy(s => s.distance).Take(maxNumberOfStops).ToList())
            {
                // Call BusAPI.RequestArrivalInfo with StopPoint.Id
                //Returns list of ArrivalInfo(next buses)
                List <ArrivalInfo> arrInfoList = stopAPI.RequestArrivalInfo(stop.naptanId);

                // for each arrival info - first order list by timetoarrival
                foreach (ArrivalInfo arrInfo in arrInfoList.OrderBy(a => a.TimeToStation).ToList())
                {
                    // Create new BusInfo
                    BusArrivalInfo busArrival = new BusArrivalInfo()
                    {
                        StopName       = stop.commonName,
                        DistanceToStop = stop.distance,
                        StopLetter     = stop.stopLetter,
                        RouteNumber    = arrInfo.LineName,
                        Destination    = arrInfo.DestinationName,
                        TimeToArrival  = arrInfo.TimeToStationInMins
                    };

                    // Add to list
                    arrivals.Add(busArrival);
                }
            }

            // return list of bus info
            return(arrivals);
        }