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); }