コード例 #1
0
        public ActionResult BusInfo(PostcodeSelection selection)
        {
            // Add some properties to the BusInfo view model with the data you want to render on the page.
            // Write code here to populate the view model with info from the APIs.
            // Then modify the view (in Views/Home/BusInfo.cshtml) to render upcoming buses

            // Validate submitted postcode
            PostcodeAPI pc        = new PostcodeAPI();
            bool        isPcValid = pc.ValidPostcode(selection.Postcode);

            // populate info object to be passed to view
            var info = new Web.ViewModels.BusInfo()
            {
                PostCode = (selection.Postcode == null) ? "" : selection.Postcode.ToUpper(),

                IsPostcodeValid = isPcValid,

                NumberOfStops = (selection.NumberOfStops == null) ? "All" : selection.NumberOfStops,
            };

            // Get arrival info by submitted postcode, only if postcode is valid
            if (isPcValid)
            {
                info.Stops = (selection.NumberOfStops == null) ?
                             Api.BusStop.GetBusStopArrivalsByPostcode(selection.Postcode) :
                             Api.BusStop.GetBusStopArrivalsByPostcode(selection.Postcode, Convert.ToInt32(selection.NumberOfStops));
            }

            return(View(info));
        }
コード例 #2
0
        private static List <StopInfo> findStops(string postcode, string type, int num)
        {
            PostcodeAPI postAPI = new PostcodeAPI();

            string  postResponse = postAPI.Execute(postcode);
            JObject json         = JObject.Parse(postResponse);

            string result = json["result"].ToString();

            PostcodeInfo postInfo = JsonConvert.DeserializeObject <PostcodeInfo>(result);

            int range = 100;

            string  tflResponse = tflAPI.stopTypes(type, range, postInfo.latitude, postInfo.longitude);
            JObject jStops      = JObject.Parse(tflResponse);

            string array = jStops["stopPoints"].ToString();

            List <StopInfo> stops = JsonConvert.DeserializeObject <List <StopInfo> >(array);

            while (stops.Count == 0 && range <= 1000)
            {
                range      += 100;
                tflResponse = tflAPI.stopTypes(type, range, postInfo.latitude, postInfo.longitude);
                jStops      = JObject.Parse(tflResponse);

                array = jStops["stopPoints"].ToString();

                stops = JsonConvert.DeserializeObject <List <StopInfo> >(array);
            }

            stops.Sort();

            return(stops);
        }
コード例 #3
0
        private static List <StopInfo> findStops(string postcode, int num)
        {
            PostcodeAPI postAPI = new PostcodeAPI();

            string  postResponse = postAPI.Execute(postcode);
            JObject json         = JObject.Parse(postResponse);

            string result = json["result"].ToString();

            PostcodeInfo postInfo = JsonConvert.DeserializeObject <PostcodeInfo>(result);

            string  tflResponse = tflAPI.stopTypes("NaptanPublicBusCoachTram", postInfo.latitude, postInfo.longitude);
            JObject jStops      = JObject.Parse(tflResponse);

            string array = jStops["stopPoints"].ToString();

            List <StopInfo> stops = JsonConvert.DeserializeObject <List <StopInfo> >(array);

            stops.Sort();

            return(stops.Take(num).ToList());
        }