public IActionResult SelectCity(string city, string state, string attraction_name) { string state_upcase = state.ToUpper(); HttpContext.Session.SetString("City", city); HttpContext.Session.SetString("State", state_upcase); HttpContext.Session.SetString("Attraction", attraction_name); HttpContext.Session.SetString("Error", ""); LatLng LatLngObject = new LatLng(); CityAttractions CityAttractionsObject = new CityAttractions(); if (!string.IsNullOrWhiteSpace(city) && !string.IsNullOrWhiteSpace(state_upcase)) { WebRequest.GetLatLongByCityStateNameAsync(city, state_upcase, LatLngResponse => { LatLngObject = LatLngResponse; }).Wait(); ViewBag.LatLong = LatLngObject; HttpContext.Session.SetString("Latitude", LatLngObject.Latitude.ToString()); HttpContext.Session.SetString("Longitude", LatLngObject.Longitude.ToString()); if (city.ToUpper() == LatLngObject.City.ToUpper() && state_upcase == LatLngObject.State) { City newCity = _context.Cities.FirstOrDefault(x => x.Name == city && x.State == state_upcase); if (newCity == null || newCity.Name == null) { newCity = new City { Name = city, State = state_upcase }; _context.Cities.Add(newCity); _context.SaveChanges(); newCity = _context.Cities.SingleOrDefault(x => x.Name == city && x.State == state); } AddAttractions(LatLngObject, attraction_name, CityAttractionsObject, newCity); } else { String error = $"No information for city: {city} in state: {state_upcase}"; HttpContext.Session.SetString("Error", error); } ViewBag.CityAttractions = CityAttractionsObject; } return(RedirectToAction("Index")); }
private void AddRestaurants(City newCity, CityAttractions CityAttractionsObject) { foreach (Attraction attraction in CityAttractionsObject.Attractions) { Restaurant newRestaurant = new Restaurant { CityId = newCity.Id, Name = attraction.Name, Vicinity = attraction.Vicinity, Latitude = attraction.Latitude, Longitude = attraction.Longitude }; _context.Restaurants.Add(newRestaurant); } _context.SaveChanges(); }
private void AddConvenienceStores(City newCity, CityAttractions CityAttractionsObject) { foreach (Attraction attraction in CityAttractionsObject.Attractions) { ConvenienceStore newConvenienceStore = new ConvenienceStore { CityId = newCity.Id, Name = attraction.Name, Vicinity = attraction.Vicinity, Latitude = attraction.Latitude, Longitude = attraction.Longitude }; _context.ConvenienceStores.Add(newConvenienceStore); } _context.SaveChanges(); }
private void AddBars(City newCity, CityAttractions CityAttractionsObject) { foreach (Attraction attraction in CityAttractionsObject.Attractions) { Bar newBar = new Bar { CityId = newCity.Id, Name = attraction.Name, Vicinity = attraction.Vicinity, Latitude = attraction.Latitude, Longitude = attraction.Longitude }; _context.Bars.Add(newBar); } _context.SaveChanges(); }
private void AddStarbucks(City newCity, CityAttractions CityAttractionsObject) { foreach (Attraction attraction in CityAttractionsObject.Attractions) { if (attraction.Name.Contains("Starbucks")) { Starbuck newStarbuck = new Starbuck { CityId = newCity.Id, Name = attraction.Name, Vicinity = attraction.Vicinity, Latitude = attraction.Latitude, Longitude = attraction.Longitude }; _context.Starbucks.Add(newStarbuck); } } _context.SaveChanges(); }
private void AddBicycleStores(City newCity, CityAttractions CityAttractionsObject) { // List<BicycleStore> bicycleStores = _context.BicycleStores.Where(x => x.CityId == newCity.Id).ToList(); // if (bicycleStores.Count() == 0) { foreach (Attraction attraction in CityAttractionsObject.Attractions) { BicycleStore newBicycleStore = new BicycleStore { CityId = newCity.Id, Name = attraction.Name, Vicinity = attraction.Vicinity, Latitude = attraction.Latitude, Longitude = attraction.Longitude }; _context.BicycleStores.Add(newBicycleStore); } _context.SaveChanges(); // } }
private void AddParks(City newCity, CityAttractions CityAttractionsObject) { // List<Park> parks = _context.Parks.Where(x => x.CityId == newCity.Id).ToList(); // if (parks.Count() == 0) { foreach (Attraction attraction in CityAttractionsObject.Attractions) { Park newPark = new Park { CityId = newCity.Id, Name = attraction.Name, Vicinity = attraction.Vicinity, Latitude = attraction.Latitude, Longitude = attraction.Longitude }; _context.Parks.Add(newPark); } _context.SaveChanges(); // } }
public static async Task GetCityAttractionAsync(LatLng latLng, string attraction, string uri, Action <CityAttractions> Callback) { string nextPageToken = null; // Create a temporary HttpClient connection. using (var Client = new HttpClient()) { try { // string placesApiKey = "AIzaSyAgsubmt3p8asFrV-x_x_cEInQokzf-X9s"; // https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-33.8670,151.1957&radius=500&types=food&name=cruise&key=YOUR_API_KEY // string uri = $"https://maps.googleapis.com/maps/api/place/nearbysearch/json?location={latLng.Latitude},{latLng.Longitude}&radius=100000&types={attraction}&key={placesApiKey}"; // https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=47.6101497,-122.2015159&radius=50000&types=park&rankby=prominence&key=AIzaSyAgsubmt3p8asFrV-x_x_cEInQokzf-X9s // string uri = $"https://maps.googleapis.com/maps/api/place/nearbysearch/json?location={latLng.Latitude},{latLng.Longitude}&types={attraction}&rankby=distance&key={placesApiKey}"; // string uri = $"https://maps.googleapis.com/maps/api/place/nearbysearch/json?location={latLng.Latitude},{latLng.Longitude}&types={attraction}&radius=3200&key={placesApiKey}"; Client.BaseAddress = new Uri(uri); HttpResponseMessage Response = await Client.GetAsync(""); // Make the actual API call. Response.EnsureSuccessStatusCode(); // Throw error if not successful. string StringResponse = await Response.Content.ReadAsStringAsync(); // Read in the response as a string. JObject CityAttractionsObject = JsonConvert.DeserializeObject <JObject>(StringResponse); // Console.WriteLine("results: " + CityAttractionsObject["results"]); // Console.WriteLine("next_page_token: " + CityAttractionsObject["next_page_token"]); if (CityAttractionsObject["next_page_token"] != null) { nextPageToken = CityAttractionsObject["next_page_token"].Value <string>(); } JArray AttractionList = CityAttractionsObject["results"].Value <JArray>(); List <Attraction> Attractions = new List <Attraction>(); foreach (JObject AttractionObject in AttractionList) { Attraction newAttraction = new Attraction { Name = AttractionObject["name"].Value <string>(), Vicinity = AttractionObject["vicinity"].Value <string>(), Latitude = AttractionObject["geometry"]["location"]["lat"].Value <double>(), Longitude = AttractionObject["geometry"]["location"]["lng"].Value <double>() }; Attractions.Add(newAttraction); } // Console.WriteLine("results: " + CityAttractionsObject["results"]); // Console.WriteLine("name: " + CityAttractionsObject["results"][0]["name"]); // Console.WriteLine("name: " + CityAttractionsObject["results"][1]["name"]); // Console.WriteLine("name: " + CityAttractionsObject["results"][2]["name"]); CityAttractions CityAttractions = new CityAttractions { Attraction = attraction, Attractions = Attractions, NextPageToken = nextPageToken }; // Finally, execute our callback, passing it the response we got. // Callback(JsonResponse); Callback(CityAttractions); } catch (HttpRequestException e) { // If something went wrong, display the error. Console.WriteLine($"Request exception: {e.Message}"); } } }
private void AddAttractions(LatLng LatLngObject, string attraction_name, CityAttractions CityAttractionsObject, City newCity) { string placesApiKey = "AIzaSyAgsubmt3p8asFrV-x_x_cEInQokzf-X9s"; string uri = $"https://maps.googleapis.com/maps/api/place/nearbysearch/json?location={LatLngObject.Latitude},{LatLngObject.Longitude}&types={attraction_name}&radius=3200&key={placesApiKey}"; // Check if the attraction has already been retrieved, and has been stored in the database. if (attraction_name == PARK) { List <Park> parks = _context.Parks.Where(x => x.CityId == newCity.Id).ToList(); if (parks.Count() != 0) { return; } } else if (attraction_name == BAR) { List <Bar> bars = _context.Bars.Where(x => x.CityId == newCity.Id).ToList(); if (bars.Count() != 0) { return; } } else if (attraction_name == BICYCLE_STORE) { List <BicycleStore> bicycleStores = _context.BicycleStores.Where(x => x.CityId == newCity.Id).ToList(); if (bicycleStores.Count() != 0) { return; } } else if (attraction_name == CAFE) { List <Cafe> cafes = _context.Cafes.Where(x => x.CityId == newCity.Id).ToList(); if (cafes.Count() != 0) { return; } } else if (attraction_name == CONVENIENCE_STORE) { List <ConvenienceStore> ConvenienceStores = _context.ConvenienceStores.Where(x => x.CityId == newCity.Id).ToList(); if (ConvenienceStores.Count() != 0) { return; } } else if (attraction_name == RESTAURANT) { List <Restaurant> Restaurants = _context.Restaurants.Where(x => x.CityId == newCity.Id).ToList(); if (Restaurants.Count() != 0) { return; } } else if (attraction_name == STARBUCKS) { uri = $"https://maps.googleapis.com/maps/api/place/nearbysearch/json?location={LatLngObject.Latitude},{LatLngObject.Longitude}&name=starbucks&radius=3200&key={placesApiKey}"; List <Starbuck> Starbucks = _context.Starbucks.Where(x => x.CityId == newCity.Id).ToList(); if (Starbucks.Count() != 0) { return; } } bool nextPage = true; while (nextPage) { WebRequest.GetCityAttractionAsync(LatLngObject, attraction_name, uri, CityAttractionsResponse => { CityAttractionsObject = CityAttractionsResponse; }).Wait(); if (CityAttractionsObject.Attraction.ToLower() == PARK) { AddParks(newCity, CityAttractionsObject); } else if (CityAttractionsObject.Attraction.ToLower() == BAR) { AddBars(newCity, CityAttractionsObject); } else if (CityAttractionsObject.Attraction.ToLower() == BICYCLE_STORE) { AddBicycleStores(newCity, CityAttractionsObject); } else if (CityAttractionsObject.Attraction.ToLower() == CAFE) { AddCafes(newCity, CityAttractionsObject); } else if (CityAttractionsObject.Attraction.ToLower() == CONVENIENCE_STORE) { AddConvenienceStores(newCity, CityAttractionsObject); } else if (CityAttractionsObject.Attraction.ToLower() == RESTAURANT) { AddRestaurants(newCity, CityAttractionsObject); } else if (CityAttractionsObject.Attraction.ToLower() == STARBUCKS) { AddStarbucks(newCity, CityAttractionsObject); } string nextPageToken = CityAttractionsObject.NextPageToken; if (nextPageToken != null) { Thread.Sleep(2000); uri = $"https://maps.googleapis.com/maps/api/place/nearbysearch/json?location={LatLngObject.Latitude},{LatLngObject.Longitude}&types={attraction_name}&radius=3200&pagetoken={nextPageToken}&key={placesApiKey}"; if (CityAttractionsObject.Attraction.ToLower() == STARBUCKS) { uri = $"https://maps.googleapis.com/maps/api/place/nearbysearch/json?location={LatLngObject.Latitude},{LatLngObject.Longitude}&name=starbucks&radius=3200&pagetoken={nextPageToken}&key={placesApiKey}"; } } else { nextPage = false; } } }