Exemple #1
0
        public List <string> GetRoute(string city, string origin, string destination)
        {
            DateTime start = DateTime.Now;
            //Monitor_Manager.AddRequestFromClientSize();
            string         key          = city + origin + destination;
            List <Station> station_list = new List <Station>(velibServiceClient.GetStations(city));

            Location origin_station      = GetTheNearestStation(station_list, origin);
            Location destination_station = GetTheNearestStation(station_list, destination);

            if (origin_station.lat == destination_station.lat && origin_station.lng == destination_station.lng)
            {
                var           resl = new List <string>();
                string        info = "You should walk to the destination\n";
                List <string> walk = GetRouteByWalk(origin, destination);
                resl.Add(info);
                resl.AddRange(walk);
                return(resl);
            }
            var           res     = new List <string>();
            List <string> orilist = GetTheRouteofTheLocationToStation(origin_station, origin);
            List <string> deslist = GetTheRouteofTheStationToLocation(destination_station, destination);

            res.AddRange(orilist);
            WebRequest request = WebRequest.Create("https://maps.googleapis.com/maps/api/directions/json?mode=bicycling&origin=" +
                                                   origin_station.lat + "," + origin_station.lng + "&destination=" + destination_station.lat + "," + destination_station.lng + "&key=" + apiKey_Google);

            try
            {
                WebResponse  response           = request.GetResponse();
                Stream       dataStream         = response.GetResponseStream();
                StreamReader reader             = new StreamReader(dataStream);
                string       responseFromServer = reader.ReadToEnd();
                JObject      route = JObject.Parse(responseFromServer);

                string routes = "";

                if (((string)route["geocoded_waypoints"][0]["geocoder_status"]).Equals("OK") &&
                    ((string)route["geocoded_waypoints"][1]["geocoder_status"]).Equals("OK"))
                {
                    if (route["routes"].HasValues)
                    {
                        routes = "Distance: " + route["routes"][0]["legs"][0]["distance"]["text"] + "\n Duration: "
                                 + route["routes"][0]["legs"][0]["duration"]["text"];
                        res.Add(routes);
                        int i = 1;
                        foreach (var step in route["routes"][0]["legs"][0]["steps"])
                        {
                            routes = i + ". " + step["html_instructions"] + "\n" + step["distance"]["text"]
                                     + "\n" + step["duration"]["text"];
                            res.Add(routes);
                            i++;
                        }
                    }
                    {
                        routes = "Bike Finish";
                        res.Add(routes);
                    }
                }
                else
                {
                    routes = "Maybe something is wrong.";
                    res.Add(routes);
                }
                res.AddRange(deslist);
                return(res);
            }
            catch (WebException wex)
            {
                Console.WriteLine("Web Exception" + wex);
                return(null);
            }
        }