Exemplo n.º 1
0
        /* Method to get a bike itinerary between two points */
        private Itinerary getBikeItinerary(Point start, Point end)
        {
            string url = "https://routing.openstreetmap.de/routed-bike/route/v1/driving/" +
                         start.longitude.ToString().Replace(",", ".") + "," +
                         start.latitude.ToString().Replace(",", ".") + ";" +
                         end.longitude.ToString().Replace(",", ".") + "," +
                         end.latitude.ToString().Replace(",", ".") +
                         "?overview=false&geometries=polyline&steps=true";
            string responseBody;

            if (cache.Get(url) == null)
            {
                HttpResponseMessage response = client.GetAsync(url).GetAwaiter().GetResult();
                response.EnsureSuccessStatusCode();
                responseBody = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
                cache.Add(url, responseBody, cacheItemPolicy);
            }
            else
            {
                responseBody = (string)cache.Get(url);
            }

            Itinerary itinerary = JsonSerializer.Deserialize <Itinerary>(responseBody);

            return(itinerary);
        }
Exemplo n.º 2
0
        /* Method to a list of routing between two addresses */
        public List <Routing> GetItineraryBetweenTwoAddress(string startingAddress, string destination)
        {
            Point startingAddressPoint = getCoordinatesFromAddress(startingAddress);
            Point destinationPoint     = getCoordinatesFromAddress(destination);

            List <Routing> routings = new List <Routing>();

            if (startingAddressPoint != null && destinationPoint != null)
            {
                WebProxyService.Station[] stations = webProxyService.GetStationsForContract(startingAddressPoint.city);

                Itinerary footItinerary = getFootItinerary(startingAddressPoint, destinationPoint);

                if (stations != null && stations.Length > 0)
                {
                    GeoCoordinate startPointCoord = new GeoCoordinate(Convert.ToDouble(startingAddressPoint.latitude), Convert.ToDouble(startingAddressPoint.longitude));
                    GeoCoordinate endPointCoord   = new GeoCoordinate(Convert.ToDouble(destinationPoint.latitude), Convert.ToDouble(destinationPoint.longitude));

                    WebProxyService.Station startPointNearestStation = getNearestAvailableStation(stations, startPointCoord, startingAddressPoint.city);
                    WebProxyService.Station endPointNearestStation   = getNearestAvailableStation(stations, endPointCoord, destinationPoint.city);

                    List <Itinerary> itineraries = new List <Itinerary>();

                    itineraries.Add(getFootItinerary(startingAddressPoint, new Point(startPointNearestStation.position.latitude, startPointNearestStation.position.longitude)));
                    itineraries.Add(getBikeItinerary(new Point(startPointNearestStation.position.latitude, startPointNearestStation.position.longitude), new Point(endPointNearestStation.position.latitude, endPointNearestStation.position.longitude)));
                    itineraries.Add(getFootItinerary(new Point(endPointNearestStation.position.latitude, endPointNearestStation.position.longitude), destinationPoint));


                    float total_duration = 0;

                    foreach (var itineray in itineraries)
                    {
                        total_duration += itineray.routes[0].duration;
                    }

                    if (footItinerary.routes[0].duration < total_duration)
                    {
                        routings.Add(createRouting("Foot", footItinerary.routes[0].duration, startingAddressPoint,
                                                   destinationPoint, footItinerary.routes[0].legs[0].steps));
                    }
                    else
                    {
                        routings.Add(createRouting("Foot", itineraries[0].routes[0].duration, startingAddressPoint,
                                                   new Point(startPointNearestStation), itineraries[0].routes[0].legs[0].steps));

                        routings.Add(createRouting("Bike", itineraries[1].routes[0].duration, new Point(startPointNearestStation),
                                                   new Point(endPointNearestStation), itineraries[1].routes[0].legs[0].steps));

                        routings.Add(createRouting("Foot", itineraries[2].routes[0].duration, new Point(endPointNearestStation),
                                                   destinationPoint, itineraries[2].routes[0].legs[0].steps));

                        stationLog.AddLogForStation(startPointNearestStation.name);
                        stationLog.AddLogForStation(endPointNearestStation.name);
                    }
                }
                else
                {
                    routings.Add(createRouting("Foot", footItinerary.routes[0].duration, startingAddressPoint,
                                               destinationPoint, footItinerary.routes[0].legs[0].steps));
                }
            }

            WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Origin", "*");

            return(routings);
        }