public async Task <Trip> SaveCity(int locationId, [FromBody] string[] names) /*First name is city, second name is country, third is place_id*/
        {
            ILocationManager m = ObjectContainer.GetLocationManager();
            Location         l = m.Get(locationId);

            ICityDbProvider c = ObjectContainer.GetCityDbProvider();

            if (names[0] == "" && names[1] == "" && names[2] == null)
            {
                l.City = null;
            }
            else
            {
                l.City = new City
                {
                    Name          = names[0],
                    CountryID     = c.GetCountryByName(names[1]).ID,
                    GooglePlaceId = names[2]
                };
            }

            m.Save(l);

            IMapsManager map  = ObjectContainer.GetMapsManager();
            ITripManager t    = ObjectContainer.GetTripManager();
            Trip         trip = t.Get(l.TripId);

            using (Trip tripX = ObjectContainer.Clone(trip)) { trip = await map.FillBorderPoints(GetUser(), tripX, l); }

            trip.ArrangePoints();

            t.Save(GetUser().ID, trip);

            return(trip);
        }
        public async Task <Country[]> ValidateCountry(string country)
        {
            HttpClient          c   = new HttpClient();
            string              key = System.IO.File.ReadAllText(Path.GetFullPath("~/../Assets/api_key.txt").Replace("~\\", ""));
            HttpResponseMessage m   = await c.GetAsync("https://maps.googleapis.com/maps/api/place/autocomplete/json?language=en&key=" + key + "&types=(regions)&input=" + country);

            string response = await m.Content.ReadAsStringAsync();

            response = FixCountryNames(response);

            ICityDbProvider cityDbProvider = ObjectContainer.GetCityDbProvider();

            CityDT o = Newtonsoft.Json.JsonConvert.DeserializeObject <CityDT>(response);

            List <Country> countries = new List <Country>();

            for (var i = 0; i < o.Predictions.Length; i++)
            {
                var guess = cityDbProvider.GetCountryByName(o.Predictions[i].structured_formatting.main_text);
                if (guess != null && !countries.Contains(guess))
                {
                    countries.Add(guess);
                }
            }

            return(countries.ToArray());
        }
        public async Task <City[]> ValidateCity(string city)
        {
            HttpClient          c   = new HttpClient();
            string              key = System.IO.File.ReadAllText(Path.GetFullPath("~/../Assets/api_key.txt").Replace("~\\", ""));
            HttpResponseMessage m   = await c.GetAsync("https://maps.googleapis.com/maps/api/place/autocomplete/json?language=en&key=" + key + "&types=(cities)&input=" + city);

            string response = await m.Content.ReadAsStringAsync();

            response = FixCountryNames(response);

            ICityDbProvider cityDbProvider = ObjectContainer.GetCityDbProvider();

            CityDT o = Newtonsoft.Json.JsonConvert.DeserializeObject <CityDT>(response);

            string[] realCities    = new string[o.Predictions.Length];
            string[] secs          = new string[o.Predictions.Length];
            string[] realCountries = new string[o.Predictions.Length];
            City[]   cities        = new City[o.Predictions.Length];

            for (var i = 0; i < o.Predictions.Length; i++)
            {
                realCities[i]    = o.Predictions[i].structured_formatting.main_text;
                secs[i]          = o.Predictions[i].structured_formatting.secondary_text;
                realCountries[i] = (secs[i] == "" || secs[i] == null) ? realCities[i] : secs[i];
                if (realCountries[i].Contains(','))
                {
                    realCountries[i] = realCountries[i].Split(',')[realCountries[i].Split(',').Length - 1];
                }
                if (realCountries[i].ElementAt(0) == ' ')
                {
                    realCountries[i] = realCountries[i].Substring(1);
                }
                cities[i] = new City
                {
                    Country       = cityDbProvider.GetCountryByName(realCountries[i]),
                    GooglePlaceId = o.Predictions[i].place_id,
                    Name          = realCities[i]
                };
            }
            return(cities);
        }
Exemple #4
0
        public async Task <BorderCrossObject> GetBorderCrossTime(Location start, Location finish, List <Location> section, int insertAt)
        {
            if (finish.InboundTravelType == TravelType.PLANE)
            {
                return(new BorderCrossObject {
                    IncrementBy = 0, CrossedAt = start.DepartureTime.Value + start.DepartureDate.Value
                });
            }
            else if (finish.InboundTravelType == TravelType.CAR || finish.InboundTravelType == TravelType.PUBLIC_TRANSPORT || finish.InboundTravelType == TravelType.BOAT)
            {
                long departure = start.DepartureDate.Value + start.DepartureTime.Value;
                long arrival   = finish.ArrivalDate.Value + finish.ArrivalTime.Value;

                if (arrival <= departure)
                {
                    return(null);
                }

                string departureId = start.City.GooglePlaceId;
                string arrivalId   = finish.City.GooglePlaceId;

                HttpClient          c   = new HttpClient();
                string              key = System.IO.File.ReadAllText(Path.GetFullPath("~/../Assets/api_key.txt").Replace("~\\", ""));
                HttpResponseMessage m;
                try
                {
                    m = await c.GetAsync("https://maps.googleapis.com/maps/api/directions/json?origin=place_id:" + departureId + "&destination=place_id:" + arrivalId + "&key=" + key);
                }
                catch (HttpRequestException e)
                {
                    return(null);
                }

                string response = await m.Content.ReadAsStringAsync();

                MapParent d = Newtonsoft.Json.JsonConvert.DeserializeObject <MapParent>(response);

                if (d.Routes == null || d.Routes.Length == 0)
                {
                    return(new BorderCrossObject {
                        IncrementBy = 0, CrossedAt = -1
                    });
                }

                MapLeg[] legs = d.Routes[0].Legs;

                //Checking if there is more than one border cross
                int crosses = 0;
                //Count the looped seconds
                long          counter            = 0;
                List <long>   durations          = new List <long>();
                long          totalTripMapLength = 0;
                List <string> countries          = new List <string>();
                for (var i = 0; i < legs.Length; i++)
                {
                    totalTripMapLength += Convert.ToInt64(legs[i].Duration.Value);

                    for (var j = 0; j < legs[i].Steps.Length; j++)
                    {
                        counter += Convert.ToInt32(legs[i].Steps[j].Duration.Value);

                        if (legs[i].Steps[j].Html_Instructions.Contains("Entering ") && !legs[i].Steps[j].Html_Instructions.Contains("Entering toll zone"))
                        {
                            crosses++;
                            durations.Add(counter);
                            countries.Add(legs[i].Steps[j].Html_Instructions.Split("Entering ")[1].Split('<')[0]);
                        }
                    }
                }

                double ratio = Convert.ToDouble(totalTripMapLength * 1000) / Convert.ToDouble(arrival - departure);

                for (var i = 0; i < countries.Count; i++)
                {
                    countries[i] = MapsController.FixCountryNames(countries[i]);
                }

                if (countries.Count > 1)
                {
                    ICityDbProvider cityDbProvider = ObjectContainer.GetCityDbProvider();

                    for (var i = countries.Count - 2; i >= 0; i--)
                    {
                        City city = new City {
                            Country = cityDbProvider.GetCountryByName(countries[i]), Name = "Transit_Country", CountryID = cityDbProvider.GetCountryByName(countries[i]).ID
                        };

                        double transitPointDateTime = start.DepartureDate.Value + start.DepartureTime.Value + durations[i] * 1000 / ratio;

                        long transitPointTime = Convert.ToInt64(transitPointDateTime % (60000 * 60 * 24));
                        long transitPointDate = Convert.ToInt64(transitPointDateTime - transitPointTime);

                        section.Insert(insertAt, new Location {
                            City = city, Transit = true, ArrivalDate = transitPointDate, ArrivalTime = transitPointTime, DepartureDate = transitPointDate, DepartureTime = transitPointTime, InboundTravelType = TravelType.CAR
                        });
                        section.Insert(insertAt, new Location {
                            IsCrossing = true, CrossedBorder = true, Transit = false, CrossedAtDate = transitPointDate, CrossedAtTime = transitPointTime
                        });
                    }

                    //Make sure to save the trip!
                }
                //Done checking if there is more than one border cross

                for (var i = 0; i < section.Count; i++)
                {
                    section[i].Position = i;
                }

                return(new BorderCrossObject
                {
                    IncrementBy = (countries.Count - 1) * 2,
                    CrossedAt = start.DepartureDate.Value + start.DepartureTime.Value + Convert.ToInt64(durations[durations.Count - 1] * 1000 / ratio)
                });
            }
            else
            {
                return(null);
            }
        }